Access objects dynamically set / created by Templates

可紊 提交于 2020-03-25 18:59:41

问题


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

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

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

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.

For clarity's sake:

The Warning Image should be replacing the History Image.


回答1:


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.



来源:https://stackoverflow.com/questions/19209962/access-objects-dynamically-set-created-by-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!