访问由模板动态设置/创建的对象

时间:2013-10-06 14:32:37

标签: wpf datagrid listcollectionview

我开始提问[{3}},但似乎不够简洁。

我有一个绑定到对象的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";

创建Job对象时,将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>

当任务的状态发生变化时,图像从运行变为完成,警告或错误。可以有许多具有不同stati的作业和组。

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

对于这一点,一切运作良好。

这是问题的来源:
如何根据要分组的作业设置组头的图像?如果任何作业有错误或警告,则应将组头图像更改为更严重。如果任何作业仍在运行(但没有错误或警告),则标题应描述正在运行。如果所有作业都成功,​​则应显示成功图像。我的问题不是逻辑,而是如何访问特定的Group Header并修改StackBox中的Image。

为了清楚起见:

enter image description here

警告图像应该替换历史图像。

1 个答案:

答案 0 :(得分:0)

我最后将一个Image属性添加到我的Job对象中。然后我使用“已加载”事件搜索所有组,并将“此”图像添加到与该组对应的作业中。