gpt4 book ai didi

wpf - 无法通过 DataTrigger 设置 ContentTemplate

转载 作者:行者123 更新时间:2023-12-02 01:38:36 25 4
gpt4 key购买 nike

我希望 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

最佳答案

我知道OP不再需要这个答案,但我想我无论如何都会回答它,以防其他人遇到同样的问题

问题中 Xaml 的唯一问题是 ContentTemplate 是在 ContentControl 上明确设置的,而不是在 Style 中设置的,这会覆盖触发器。在样式中设置它可以解决问题

<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>

关于wpf - 无法通过 DataTrigger 设置 ContentTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2090148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com