WPF DataTemplate 绑定取决于属性的类型 [英] WPF DataTemplate Binding depending on the type of a property

查看:35
本文介绍了WPF DataTemplate 绑定取决于属性的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到分层数据模板的对象集合,我的每个对象都有一个属于某种类型的属性(我们称之为属性A").这种类型因每个对象而异.

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.

如果数据模板包含图像和一些文本,根据属性A"的类型更改模板中显示的图像的最佳方法是什么.

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

我知道我可以将其粘贴到转换器中并在代码中手动进行绑定转换,但是使用 WPF 中可用的所有绑定工具,我认为可能有更好的方法.

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.

推荐答案

如果您创建本地数据模板并使用 ContentPresenter,那么在您的数据模板中执行此操作非常简单.此模板呈现 MyObject 类型的对象,显示一个图像,其来源由 A 属性的类型确定,紧挨着 TextBlock 显示Text 属性的内容:

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>

如果你想用样式来代替,你会遇到一个问题,因为数据触发器要查看属性值,以及A的类型 属性本身并不是作为属性公开的.

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(); } }

(当 A 的值发生变化时,您还需要为 AType 引发 PropertyChanged.)一旦你完成了这个,您应该能够以某种样式实现数据触发器,例如:

(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆