访问对象动态设置/通过模板创建(Access objects dynamically set / c

2019-10-29 16:44发布

我开始一个问题在这里 ,但它似乎没有足够简洁。

我已绑定到一个对象(作业)一个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;

作业数据:

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>

当一个任务的状态已经改变了图像从运行到完成,警告或错误更改。 可以有许多工作和组具有不同为static。

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

这一点,一切运作良好。

这就是问题的物品:
如何设置进行分组组头的基础上,乔布斯的形象呢? 如果任何工作有错误或警告组头的形象应该是改为更加严重。 如果有任何作业仍在运行(但没有错误或警告),页眉应该描写运行。 如果所有作业都成功,​​成功的形象应该是所示。 我的问题是不是逻辑,而是如何访问特定的组页眉和修改图像的StackBox。

为了清楚起见:

警告图像,应更换历史记录图像。

Answer 1:

我结束了添加图像属性,以我的工作对象。 然后我用“装”事件搜索所有组,并添加“这种”图像对应于本集团的工作。



文章来源: Access objects dynamically set / created by Templates