WPF:当某个值发生变化时重新应用 DataTemplateSelector [英] WPF: Reapply DataTemplateSelector when a certain value changes
问题描述
这是我拥有的 XAML:
So here is the XAML that I have:
<ItemsControl ItemsSource="{Binding Path=Groups}" ItemTemplateSelector="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=ListTemplateSelector}"/>
这是我的 ListTemplateSelector 类:
Here is my ListTemplateSelector class:
public class ListTemplateSelector : DataTemplateSelector {
public DataTemplate GroupTemplate { get; set; }
public DataTemplate ItemTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container) {
GroupList<Person> list = item as GroupList<Person>;
if (list != null && !list.IsLeaf)
return GroupTemplate;
return ItemTemplate;
}
}
GroupTemplate 数据模板在其内部引用了 ListTemplateSelector,所以这就是我设置它的原因.这是我可以组合在一起的唯一递归黑客.但这不是我遇到的问题.
The GroupTemplate data template references the ListTemplateSelector inside itself, so this is why I have set up like I have it set up. It's the only recursive hack I could put together. But that's not the problem I'm having.
我的问题是,当 IsLeaf 属性更改时,我想从 ItemTemplate 更改为 GroupTemplate.自从它第一次读取属性以来,这是第一次运行得很好.但是一旦这个属性发生变化,模板选择器就不会被重新应用.现在,我可以使用触发器绑定到值并适当地设置项目模板,但我需要能够为每个项目设置不同的模板,因为它们可能处于不同的状态.
My problem is, I want to change from ItemTemplate to GroupTemplate when the IsLeaf property changes. This works beautifully the very first time since it reads the property the first time. But once this property changes, the template selector doesn't get reapplied. Now, I could use triggers to bind to the value and set the item template appropriately, but I need to be able to set a different template for each item, as they could be in a different state.
例如,假设我有一个这样的组列表:
For instance, say I have a list of groups like this:
第 1 组:IsLeaf = false,因此模板 = GroupTemplate
Group 1: IsLeaf = false, so template = GroupTemplate
第 2 组:IsLeaf = true,所以模板 = ItemTemplate
Group 2: IsLeaf = true, so template = ItemTemplate
第 3 组:IsLeaf = false,因此模板 = GroupTemplate
Group 3: IsLeaf = false, so template = GroupTemplate
并且一旦 group 1 的 IsLeaf 属性更改为 true,模板需要自动更改为 ItemTemplate.
And once group 1's IsLeaf property changes to true, the template needs to automatically change to ItemTemplate.
这是我的临时解决方案.有什么更好的方法吗?
Here is my temporary solution. Any better way to do it?
<ItemsControl ItemsSource="{Binding Path=Groups}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding}">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate" Value="{DynamicResource ItemTemplate}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsLeaf}" Value="False">
<Setter Property="ContentTemplate" Value="{DynamicResource GroupTemplate}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
推荐答案
关于您的 EDIT,DataTemplate Trigger
不是使用 Style
就足够了吗?即:
Regarding your EDIT, wouldn't a DataTemplate Trigger
be enough instead of using a Style
? That is:
<ItemsControl ItemsSource="{Binding Path=Groups}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl x:Name="cc" Content="{Binding}" ContentTemplate="{DynamicResource ItemTemplate}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=IsLeaf}" Value="False">
<Setter TargetName="cc" Property="ContentTemplate" Value="{DynamicResource GroupTemplate}"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这篇关于WPF:当某个值发生变化时重新应用 DataTemplateSelector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!