【问题标题】:Update databinding for different elements in observable collection更新可观察集合中不同元素的数据绑定
【发布时间】:2012-06-04 21:38:08
【问题描述】:

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

【问题讨论】:

  • 它们是从一个通用接口扩展而来的吗? Dibs 访问者模式。
  • 放弃我说的话。正确答案已发布。 +1 给它。

标签: c# wpf data-binding collections


【解决方案1】:

使用不带x:KeyDataTemplate,但带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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    相关资源
    最近更新 更多