问题描述
我使用以下资源(其工作正常)在GroupItem中使用的StackPanel中设置了图像的路径:
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>
在这个DataGrid中使用哪一个:
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>
DataView通过以下代码分组:
The DataView is grouped via this code:
ListCollectionView collection = new ListCollectionView(JobData);
collection.GroupDescriptions.Add(new PropertyGroupDescription("Name"));
JobHistory.ItemsSource = collection;
我的问题:如何在StackPanel中动态设置图像源?
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>
编辑1:
使用:
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建议的作品。
as user2760623 suggested works.
我的问题依然存在。在任何给定的时间,我有多个行按名称分组。也可以有几个不同的组。根据作业当前状态,我想更改GroupItem标题中的图像。那么我该如何弄清楚哪个标题是正确的标题,如何操作一个单独的标题?
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?
推荐答案
Put the image source as a dynamic resource, and then you can change it. Just do the following:
- 定义命名空间 -
xmlns:clr =clr-namespace:System; assembly =
。 - 添加为资源 -
< clr:String x:Key =imageSource>路径... < / clr:String>
。 - 图像本身 -
< Image Source ={DynamicResource ResourceKey = imageSource}
。 - 当你想改变它 -
this.Resources [imageSource] =另一个路径... / code>。
- Define the namespace -
xmlns:clr="clr-namespace:System;assembly=mscorlib"
. - Add as resource -
<clr:String x:Key="imageSource" >the path...</clr:String>
. - And the image itself -
<Image Source="{DynamicResource ResourceKey=imageSource}"
. - And when you want to change it -
this.Resources["imageSource"] = "another path..."
.
你也可以做同样的概念,把整个图像作为资源(而不只是图像路径),比您不需要添加命名空间(上面的数字1)。并将其作为ContentControl的内容 -
< ContentControl Content ={StaticResource ResourceKey = image}/>
。
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}"/>
.
这篇关于在项目模板中动态设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!