问题描述
我有内容presenter与DataTemplateSelector:
I have ContentPresenter with DataTemplateSelector:
...
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
var model = item as ItemControlViewModel;
if (model.CurrentStatus == PrerequisitesStatus.Required)
{
return RequiredTemplate;
}
if (model.CurrentStatus == PrerequisitesStatus.Completed)
{
return FinishedTemplate;
}
...
return InProgressTemplate;
}
当CurrentStatus改变时,OnPropertyChanged称为
When CurrentStatus is changed, OnPropertyChanged is called.
我需要以某种方式当属性更改为触发此DataTemplateSelector和更改内容presenter的DataTemplate。有什么建议?
I need somehow to trigger this DataTemplateSelector when the property is changed and change ContentPresenter DataTemplate. Any suggestions?
Threre有类似的问题:
1
2,但我不希望使用任何DataTriggers,因为太多的状态。
Threre are similar questions:12, but I don't want to use any DataTriggers, because of too much states.
试图与DataTriggers玩
Tried to play with DataTriggers
<ContentPresenter
Grid.Column="1"
Height="16"
Width="16"
Margin="3">
<ContentPresenter.Triggers>
<DataTrigger Binding="{Binding Path=CurrentStatus}" Value="0">
<Setter Property="ContentPresenter.ContentTemplate" Value="{StaticResource ResourceKey=_requiredStatusTemplate}" />
</DataTrigger>
</ContentPresenter.Triggers>
</ContentPresenter>
但得到了一个错误:
Triggers集合成员必须是类型的EventTrigger的:(
But got an error:Triggers collection members must be of type EventTrigger :(
推荐答案
根据您的要求与意见datatriggers一个例子,在这里你是:
As you requested an example with datatriggers in the comments, here you are:
一个FrameworkElement的只能有EventTriggers,因此,你得到错误信息的 Triggers集合成员必须是类型的EventTrigger 的
A FrameworkElement can only have EventTriggers, therefore you get the error Message Triggers collection members must be of type EventTrigger
和也不会直接使用内容presenter,是指一个的ControlTemplate内使用。如果你想拥有动态内容更好的使用ContentControl中。
见<一href=\"http://stackoverflow.com/questions/1287995/whats-the-difference-between-contentcontrol-and-content$p$psenter\">What's ContentControl中和内容presenter区别?
And also don't use a ContentPresenter directly, it is meant to be used inside a ControlTemplate. Better use a ContentControl when you want to have dynamic content.See What's the difference between ContentControl and ContentPresenter?
和终于在这里有一个建议您DataTrigger问题。我已经把它的风格里面重用....
And finally here's a suggestion to your DataTrigger issue. I have put it inside a style for reusability ....
XAML:
<Window x:Class="WpfApplication88.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate x:Key="requiredTemplate">
<TextBlock Text="requiredTemplate"></TextBlock>
<!--your stuff here-->
</DataTemplate>
<DataTemplate x:Key="completedTemplate">
<TextBlock Text="CompletedTemplate"></TextBlock>
<!--your stuff here-->
</DataTemplate>
<Style x:Key="selectableContentStyle" TargetType="{x:Type ContentControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=CurrentStatus}" Value="Required">
<Setter Property="ContentTemplate" Value="{StaticResource requiredTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=CurrentStatus}" Value="Completed">
<Setter Property="ContentTemplate" Value="{StaticResource completedTemplate}" />
</DataTrigger>
<!-- your other Status' here -->
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<ContentControl Width="100" Height="100" Style="{StaticResource selectableContentStyle}"/>
</Grid>
</Window>
这篇关于如何触发DataTemplateSelector属性发生变化时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!