我想使用模板化的ComboBoxItems,其中包括一个图像和一个标签。如果我将模板分配给ComboBoxItem,我是否可以以某种方式设置图像的Source属性?目标是在不同的ComboBoxItems中使用相同的模板,但每个项目中的图片不同。
我还考虑过在模板中绑定Image.Source属性,但这会失败,因为“父”ComboBoxItem显然没有我可以绑定到的Source属性。
下面的代码说明了我的问题:
<Style x:Key="ComboBoxPictureItem" TargetType="{x:Type ComboBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBoxItem">
<StackPanel Orientation="Horizontal">
<Image x:Name="StatusImage" />
<Label x:Name="StatusLabel" Content="Green"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ComboBox>
<ComboBoxItem Style="{StaticResource ResourceKey=ComboBoxPictureItem}"
-> sth. like: StatusImage.Source="PathToMyImage.png"/>
</ComboBox>
谢谢!