我希望ContentTemplate
根据DataTrigger
中的值而变化
是的,我考虑使用DataTemplateSelector
,但现在我需要一个DataTrigger
或更好的MultiDataTrigger
。
请查看以下示例应用,DataTemplate
不会更改:
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:WpfApplication1">
<StackPanel>
<CheckBox IsChecked="{Binding BoolProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:Window1}}}" Content="BoolProperty"/>
<ContentControl Content="{Binding BoolProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:Window1}}}">
<ContentControl.ContentTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding BoolProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:Window1}}}" Content="Template 1"/>
</DataTemplate>
</ContentControl.ContentTemplate>
<ContentControl.Resources>
<DataTemplate x:Key="Template2">
<CheckBox IsChecked="{Binding BoolProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:Window1}}}" Content="Template 2"/>
</DataTemplate>
</ContentControl.Resources>
<ContentControl.Style>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding BoolProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:Window1}}}" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource Template2}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
<Button Name="btnSwitch" Content="Switch"/>
</StackPanel>
</Window>
Partial Class Window1
Public Property BoolProperty() As Boolean
Get
Return GetValue(BoolPropertyProperty)
End Get
Set(ByVal value As Boolean)
SetValue(BoolPropertyProperty, value)
End Set
End Property
Public Shared ReadOnly BoolPropertyProperty As DependencyProperty = DependencyProperty.Register("BoolProperty", GetType(Boolean), GetType(Window1), New FrameworkPropertyMetadata(False))
Private Sub btnSwitch_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSwitch.Click
BoolProperty = Not BoolProperty
End Sub
End Class
答案 0 :(得分:16)
我知道OP已经不再使用这个答案了,但我想我还是会回答它,以防其他人遇到同样的问题
问题中Xaml的唯一问题是ContentTemplate在ContentControl上设置为explicilty而不是Style,这会覆盖Trigger。相反,在样式中设置它可以解决问题
<ContentControl Content="{Binding BoolProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:Window1}}}">
<ContentControl.Resources>
<DataTemplate x:Key="Template2">
<CheckBox IsChecked="{Binding BoolProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:Window1}}}" Content="Template 2"/>
</DataTemplate>
</ContentControl.Resources>
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<CheckBox IsChecked="{Binding BoolProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:Window1}}}" Content="Template 1"/>
</DataTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding BoolProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:Window1}}}" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource Template2}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
答案 1 :(得分:10)
这对我有用:
<ContentControl Content="{Binding SomeBool}">
<ContentControl.Resources>
<DataTemplate x:Key="PinkTemplate">
<TextBlock Text="{Binding}" Background="Pink" />
</DataTemplate>
<DataTemplate x:Key="LimeTemplate">
<TextBlock Text="{Binding}" Background="Lime" />
</DataTemplate>
</ContentControl.Resources>
<ContentControl.ContentTemplate>
<DataTemplate>
<ContentControl Name="cc"
Content="{Binding}"
ContentTemplate="{StaticResource PinkTemplate}" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding}" Value="True">
<Setter TargetName="cc"
Property="ContentTemplate"
Value="{StaticResource LimeTemplate}" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
请注意,我的DataTemplate是另一个ContentControl,它允许我的DataTemplate.Triggers对该(嵌套)ContentControl的ContentTemplate进行操作。