2013-10-06 40 views
0

我開始了一個問題here,但它似乎不夠簡潔。模板動態設置/創建的訪問對象

我已綁定到一個對象(作業)一個DataGrid:

private String resultImagePath; // The Image to be shown in the DataGrid showing the status 
private String name; // The Job container's name 
private String jobDescription; // A Sub Task 
// AND the corresponding Public Properties implementing iPropertyChange 

public int Sequence; // For sorting purposses 
// The paths to the icons 
public String ErrorPath = "pack://application:,,,/Resources/Flag_Red.ico"; 
public String SuccessPath = "pack://application:,,,/Resources/Flag_Green.ico"; 
public String WarningPath = "pack://application:,,,/Resources/Flag_Yellow.ico"; 
public String RunningPath = "pack://application:,,,/Resources/Flag_Running.ico"; 
public String HistoryPath = "pack://application:,,,/Resources/History.ico.ico"; 

當創建一個作業對象它得到ResultImagePath設置爲RunningPath。喬布斯正在使用分組:

collection = new ListCollectionView(JobData); 
collection.GroupDescriptions.Add(new PropertyGroupDescription("Name")); 
JobHistory.ItemsSource = collection; 

JobData:

ObservableCollection<Job> JobData = new ObservableCollection<Job>(); 

JobHistory是使用樣式和模板在DataGrid:

<UserControl.Resources> 
    <Image x:Key="image" Source="pack://application:,,,/Resources/History.ico" Height="18" Width="18" Margin="2,0"/> 
    <Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type GroupItem}"> 
        <Expander Name="expander" IsExpanded="True" > 
         <Expander.Header> 
          <StackPanel Orientation="Horizontal"> 
           <ContentControl Content="{StaticResource ResourceKey=image}"/> 
           <TextBlock Text="{Binding Name}" Padding="2,0"/> 
          </StackPanel> 
         </Expander.Header> 
         <ItemsPresenter /> 
        </Expander> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</UserControl.Resources> 

<DataGrid Name="JobHistory" CanUserAddRows="False" AutoGenerateColumns="False" ColumnWidth="*" 
      CanUserDeleteRows="False" ItemsSource="{Binding}" Grid.Row="2" 
      Grid.ColumnSpan="5" CanUserResizeRows="False" 
      Grid.RowSpan="2" IsTextSearchEnabled="True" VerticalScrollBarVisibility="Visible" > 
    <DataGrid.GroupStyle> 
     <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}"> 
      <GroupStyle.Panel> 
       <ItemsPanelTemplate> 
        <DataGridRowsPresenter/> 
       </ItemsPanelTemplate> 
      </GroupStyle.Panel> 
     </GroupStyle> 
    </DataGrid.GroupStyle> 
    <DataGrid.Columns> 
     <DataGridTemplateColumn Header="Status" Width="Auto" IsReadOnly="True"> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <Image Source="{Binding ResultImagePath}" Height="18" Width="18"/> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 
     <DataGridTextColumn Header="Job description" Binding="{Binding JobDescription}"/> 
    </DataGrid.Columns> 
</DataGrid> 

當任務的狀態發生了變化的影像從運行變化完成,警告或錯誤。可以有許多不同職位和職位的團隊。

SomeTask.ResultImagePath = job.SuccessPath; 
... 
<DataTemplate> 
    <Image Source="{Binding ResultImagePath}" Height="18" Width="18"/> 
</DataTemplate> 

到這一點,一切運作良好。

這是問題的來源:
如何根據作業進行分組來設置組頁眉的圖像?如果有任何作業出現錯誤或警告,則組頁眉的圖像應更改爲更嚴重。如果任何作業仍在運行(但沒有錯誤或警告),則標題應該描述運行。如果所有喬布斯成功,則應顯示成功圖片。我的問題不是邏輯,而是如何訪問特定的組頁眉並修改StackBox中的圖像。

爲清楚起見:

enter image description here

告警圖像應更換歷史記錄圖像。

+1

不要在程序代碼中操作UI元素。如果在「DataTemplate」中,則更少。你可以爲此使用'DataTriggers'。 –

+0

請你詳細說明一下。我是WPF的新手...我在哪裏觸發,怎麼觸發? – Daro

回答

0

我結束了向我的Job對象添加一個Image屬性。然後,我使用「已加載」事件搜索所有組,並將「此」圖像添加到與該組對應的作業。