我有一个名为 Cell 的类,它有两个属性。一个称为 int?
类型的 Value
,另一个称为 ObservableCollection>
类型的 Candidates
在初始化过程中我是利用 DataTemplateSelector 在两个不同场景的两个数据模板之间进行选择。
如果 Value 属性有值,则应使用模板 A 来呈现 Cell 类。
但是,如果 Value 属性为 null,则应使用模板 B 来呈现 Cell 类。
虽然这在初始化期间工作得很好,但是在运行时,当 Value 属性的值实际更改时,模板不会再更改。
使用 DataTemplateSelector 的方法是动态更改 DataTemplates 的错误方法吗?你建议我应该做什么?
非常感谢,
I have a class called Cell with two properties. One is called Value
of type int?
And the other is called Candidates of type ObservableCollection<ObservableCollection<Candidate>>
during the initialization I am utilizing a DataTemplateSelector to choose between two datatemplates for two different scenarios.
If the Value property has a value then template A should be used to present the Cell class.
However if the Value property is null Then template B should be used to present the Cell class.
While this works perfectly fine during the initialization however during the runtime the templates don't change any more when the value of the Value property actually changes.
Is the approach of using DataTemplateSelector the wrong approach to change DataTemplates dynamically? What would you recommend I should do?
Many thanks,
发布评论
评论(1)
虽然看起来 DataTemplateSelector 方法应该有效,但另一种尝试方法是使用 DataTrigger 代替。您可以创建一个当
Value
为null
时触发的样式,并根据该值设置控件的模板。While it seems like the
DataTemplateSelector
approach should work, another way to try would be to use a DataTrigger instead. You could create a style that triggers when theValue
isnull
, and set the template of the control based on that value.