更新可观察集合中不同元素的数据绑定

时间:2012-05-30 15:56:06

标签: c# wpf data-binding collections

到目前为止,我有一个可观察的集合,其中只有一种类型的对象,但现在我有第二种类型的对象。我已将一些wpf元素绑定到此集合的元素,我现在想要的是输入表单根据它在集合中绑定的对象类型而改变。 最好的方法是什么?

1 个答案:

答案 0 :(得分:4)

使用DataTemplate而不是x:Key,但使用DataType={x:Type typename}。为集合中的每种类型编写一个数据模板。然后,WPF会自动选择与集合中项目类型匹配的数据模板。

示例:

<DataTemplate DataType="{x:Type local:StringType}">
  <TextBox Text="{Binding Text}" />
</DataTemplate>
<DataTemplate DataType="{x:Type local:BooleanType}">
  <CheckBox IsChecked="{Binding Value}" />
</DataTemplate>
相关问题