Dynamically set a property in an Item Template

百般思念 提交于 2019-12-13 05:35:15

问题


I set an image path of an Image in a StackPanel used in a GroupItem using the following resource (which as is works fine):

<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">
                            <Image Source="pack://application:,,,/Resources/History.ico" Margin="2,0"  
                                   Width="18" Height="18" ></Image>
                            <TextBlock Text="{Binding Name}" Padding="2,0"/>
                        </StackPanel>
                    </Expander.Header>
                    <ItemsPresenter />
                </Expander>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Which is used in this DataGrid:

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

The DataView is grouped via this code:

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

My Question: How can I dynamically set the image Source in the StackPanel?

<StackPanel Orientation="Horizontal">
  <Image Source="pack://application:,,,/Resources/History.ico" Margin="2,0"  
     Width="18" Height="18" ></Image>
  <TextBlock Text="{Binding Name}" Padding="2,0"/>
</StackPanel>

Edit 1: Using:

<UserControl.Resources>
            <Image x:Key="image" Source="pack://application:,,,/Resources/History.ico" Height="18" Width="18" Margin="2,0"/>
</UserControl.Resources>


<StackPanel Orientation="Horizontal">
    <ContentControl Content="{StaticResource ResourceKey=image}"/>
      Width="18" Height="18" ></Image>
    <TextBlock Text="{Binding Name}" Padding="2,0"/>
</StackPanel>

as user2760623 suggested works.

My Problem however remains. At any given time I have multiple rows grouped by "Name". There can also be several different Groups. Depending on the Jobs current Status, I would like to Change the Image in the GroupItem Header. So how do I figure out which header is the "right" Header, and how do I manipulate exactly that one single Header?


回答1:


Put the image source as a dynamic resource, and then you can change it. Just do the following:

  1. Define the namespace - xmlns:clr="clr-namespace:System;assembly=mscorlib".
  2. Add as resource - <clr:String x:Key="imageSource" >the path...</clr:String>.
  3. And the image itself - <Image Source="{DynamicResource ResourceKey=imageSource}".
  4. And when you want to change it - this.Resources["imageSource"] = "another path...".

You can also do the same concept just put the whole image as a resource (instead of just the image path), than you don't need to add the namespace (number 1 above). And put it as a Content of a ContentControl - <ContentControl Content="{StaticResource ResourceKey=image}"/>.



来源:https://stackoverflow.com/questions/19204206/dynamically-set-a-property-in-an-item-template

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