[英]DataTemplateSelector not working if ItemContainerStyle is set
I have two Listboxes, default and custom. 我有两个列表框,默认和自定义。 One uses DataTemplateSelector correctly and other just uses default DataTemplates never calling selector;
一个正确使用DataTemplateSelector,其他只使用默认的DataTemplates从不调用选择器;
//shows correctly
<ListBox Name="testlb" ItemTemplateSelector="{StaticResource ffDataTemplateSelector}"/>
//now showing correctly (using default DataTemplates instead of selector)
<local:FFBox x:Name="myFFBox" ItemTemplateSelector="{StaticResource ffDataTemplateSelector}" ItemContainerStyle="{StaticResource FFItemStyle}" />
Both have same source 两者都有相同的来源
testlb.ItemsSource = CollectionViewSource.GetDefaultView(FileCollectionView);
myFFBox.ItemsSource = CollectionViewSource.GetDefaultView(FileCollectionView);
Obviously there is nothing wrong with DataTemplateSelector as it works correctly on testlb 显然,DataTemplateSelector没有任何问题,因为它在testlb上正常工作
Problem is ItemContainerStyle="{StaticResource FFItemStyle}"
which I use to define overall look for each ListBoxItem, contains triggers, animation etc. If it is present the Selector does not work. 问题是
ItemContainerStyle="{StaticResource FFItemStyle}"
,我用它来定义每个ListBoxItem的整体外观,包含触发器,动画等。如果它存在,则Selector不起作用。
<Style x:Key="FFItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid x:Name="mygrid">
<ContentPresenter x:Name="ContentPresenter"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"/>
How do I keep ItemContainerStyle and still be able to change DataTamplates with DataTemplateSelector? 如何保留ItemContainerStyle并仍然可以使用DataTemplateSelector更改DataTamplates?
EDIT: Solved, we should keep it as this is one of those illogical and not well documented things in wpf. 编辑:解决了,我们应该保留它,因为这是wpf中那些不合逻辑且没有很好记录的事情之一。
Got it: If you have ItemContainerStyle defined, instead of ItemTemplateSelector one needs to use ContentTemplateSelector in ContentPresenter with DataTemplateSelector. 得到它:如果你定义了ItemContainerStyle,而不是ItemTemplateSelector,则需要在ContentPresenter中使用ContentTemplateSelector和DataTemplateSelector。
<ContentPresenter x:Name="ContentPresenter" Content="{TemplateBinding Content}"
ContentTemplateSelector="{StaticResource ffDataTemplateSelector}"
mmm code looks ok but have you tried setting ItemTemplateSelector it like this mmm代码看起来没问题,但你尝试过像这样设置ItemTemplateSelector
in Generic.xaml 在Generic.xaml中
<Setter Property="ItemTemplateSelector">
<Setter.Value>
<helpers:ffDataTemplateSelector x:Name="ffDataTemplateSelector" />
</Setter.Value>
</Setter>
where helpers is set like so at top of Generic.xaml 在Generic.xaml顶部设置帮助程序的位置
xmlns:helpers="using:[Namespace where ffDataTemplateSelector resides]"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.