WPF DataTemplate根据属性的类型绑定 [英] WPF DataTemplate Binding depending on the type of a property
问题描述
如果数据模板包含图像和一些文本,那么最好的方法是更改显示在基于属性A的模板。
我知道我可以把它粘贴到转换器中,并在代码中手动进行绑定翻译,但是所有的绑定在WPF中提供的设施,我认为这可能是一个更好的方法。
在数据模板中执行此操作非常简单,如果您创建本地数据模板并使用 ContentPresenter
。该模板显示类型为 MyObject
的对象,显示一个图像,其图像的来源取决于 A
属性的类型 TextBlock
显示 Text
属性的内容:
< DataTemplate DataType ={x:Type MyObject}>
< StackPanel Orientation =Horizontal>
< StackPanel.Resources>
< DataTemplate DataType ={x:Type Thing1}>
< Image Source =thing1.png/>
< / DataTemplate>
< DataTemplate DataType ={x:Type Thing2}>
< Image Source =thing2.png/>
< / DataTemplate>
< /StackPanel.Resources>
< ContentPresenter Content ={Binding A}/>
< TextBlock Text ={Binding Text}/>
< / StackPanel>
< / DataTemplate>
如果要使用样式来代替,那么您将遇到问题,因为数据触发器要查看属性值,并且 A
属性的类型本身不会作为属性公开。
除非你实现一个:
public Type AType {get {return A.GetType(); }}
(您还需要提高 PropertyChanged
AType
当 A
的值更改时。)一旦你这样做,你应该能够以风格实现数据触发,例如:
< Style TargetType =Image>
< Setter Property =SourceValue =default.png/>
< Style.Triggers>
< DataTrigger Binding ={Binding AType}Value ={x:Type Thing1}>
< Setter Property =SourceValue =thing1.png/>
< / DataTrigger>
< DataTrigger Binding ={Binding AType}Value ={x:Type Thing2}>
< Setter Property =SourceValue =thing2.png/>
< / DataTrigger>
< /Style.Triggers>
< / Style>
I have a collection of objects bound to a hierarchical data template, each of my objects have a property on them (lets call it Property "A") that is of a certain type. This type varies among each of the objects.
If the data template contains an image and some text, what would be the best way to change the image that is displayed in the template based on the type of property "A".
I know I could just stick this into a converter and do the binding translation manually in code, but with all the binding facilities available in WPF, I think theres probably a better way.
It's pretty simple to do this within your data template, if you create local data templates and use a ContentPresenter
. This template presents objects of type MyObject
, displaying an image whose source is determined by the type of the A
property next to a TextBlock
that displays the content of the Text
property:
<DataTemplate DataType="{x:Type MyObject}">
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<DataTemplate DataType="{x:Type Thing1}">
<Image Source="thing1.png"/>
</DataTemplate>
<DataTemplate DataType="{x:Type Thing2}">
<Image Source="thing2.png"/>
</DataTemplate>
</StackPanel.Resources>
<ContentPresenter Content="{Binding A}"/>
<TextBlock Text="{Binding Text}"/>
</StackPanel>
</DataTemplate>
If you want to use styles to do this instead, you're going to run into a problem, because data triggers want to look at property values, and the type of the A
property is not, itself, exposed as a property.
Unless, of course, you implement one:
public Type AType { get { return A.GetType(); } }
(You'll also need to raise PropertyChanged
for AType
when the value of A
changes.) Once you've done this, you should be able to implement a data trigger in a style, e.g.:
<Style TargetType="Image">
<Setter Property="Source" Value="default.png"/>
<Style.Triggers>
<DataTrigger Binding="{Binding AType}" Value="{x:Type Thing1}">
<Setter Property="Source" Value="thing1.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding AType}" Value="{x:Type Thing2}">
<Setter Property="Source" Value="thing2.png"/>
</DataTrigger>
</Style.Triggers>
</Style>
这篇关于WPF DataTemplate根据属性的类型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!