【发布时间】:2013-10-06 14:32:36
【问题描述】:
我开始了一个问题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";
创建 Job 对象时,它会将 ResultImagePath 设置为 RunningPath。作业分组使用:
collection = new ListCollectionView(JobData);
collection.GroupDescriptions.Add(new PropertyGroupDescription("Name"));
JobHistory.ItemsSource = collection;
工作数据:
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>
到目前为止,一切都很好。
这就是问题所在:
如何根据正在分组的作业设置组标题的图像?如果任何作业有错误或警告,则应将组标题的图像更改为更严重的。如果任何作业仍在运行(但没有错误或警告),标题应描述正在运行。如果所有作业都成功,则应显示成功图像。我的问题不是逻辑,而是如何访问特定的 Group Header 并修改 StackBox 中的 Image。
为了清楚起见:
警告图像应该替换历史图像。
【问题讨论】:
-
不要在程序代码中操作 UI 元素。如果在
DataTemplate内,则更少。您可以为此使用DataTriggers。 -
请您详细说明一下。我是 WPF 的新手......我应该在哪里以及如何触发?
标签: wpf datagrid listcollectionview