访问由模板动态设置/创建的对象 [英] Access objects dynamically set / created by Templates

查看:163
本文介绍了访问由模板动态设置/创建的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此处中提出了一个问题,但它没有我似乎很简洁。

I began a question here, but it doesn't seem to be concise enough.

我有一个DataGrid绑定到一个对象(Job):

I have a DataGrid Bound to an object (Job):

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。工作分组使用:

When a Job object is created it gets ResultImagePath set to RunningPath. The Jobs are Grouped using:

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

JobData:

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

JobHistory是使用样式和模板的DataGrid:

JobHistory is the DataGrid using styles and templates:

<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>

当任务的状态已更改时,图像从运行更改为完成,警告或错误。可以有很多不同职位的工作和团队。

When a task's Status has changed the Image is changed from running to complete, warning or error. There can be many Jobs and Groups with differing stati.

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

对于这一点,一切都很好。

To this Point all works well.

这是问题所在:

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

This is where the question Comes in:
How can I set the Image of the Group Header based on the Jobs being grouped? If any Job has errors or warnings the Group Header's Image should be changed to the more serious. If any Jobs are still running (but no Errors or warnings), the Header should depict Running. If all Jobs are successful the Success Image should be shown. My Problem is not the logic, but how to access the specific Group Header and modify the Image in the StackBox.

为了清楚起见:

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

The Warning Image should be replacing the History Image.

推荐答案

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

I wound up adding an Image property to my Job object. Then I used the "Loaded" event to search all Groups, and add "this" Image to the Job corresponding to the Group.

这篇关于访问由模板动态设置/创建的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆