问题描述
我要的的ContentTemplate
按照价值来改变 DataTrigger
。结果
是的,我认为使用 DataTemplateSelector
,但现在我需要一个 DataTrigger
或更好说了 MultiDataTrigger
。
请看看下面的示例应用程序中,的DataTemplate
并没有改变:
<窗口x:类=窗口1
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
的xmlns:SRC =CLR的命名空间:WpfApplication1>
<&StackPanel的GT;
<复选框器isChecked ={结合BoolProperty,的RelativeSource = {的RelativeSource FindAncestor,AncestorType = {X:类型SRC:窗口1}}}CONTENT =BoolProperty/>
< ContentControl中CONTENT ={结合BoolProperty,的RelativeSource = {的RelativeSource FindAncestor,AncestorType = {X:类型SRC:窗口1}}}>
< ContentControl.ContentTemplate>
<&DataTemplate的GT;
<复选框器isChecked ={结合BoolProperty,的RelativeSource = {的RelativeSource FindAncestor,AncestorType = {X:类型SRC:窗口1}}}CONTENT =模板1/>
< / DataTemplate中>
< /ContentControl.ContentTemplate>
< ContentControl.Resources>
<的DataTemplate X:键=则Template2>
<复选框器isChecked ={结合BoolProperty,的RelativeSource = {的RelativeSource FindAncestor,AncestorType = {X:类型SRC:窗口1}}}CONTENT =模板2/>
< / DataTemplate中>
< /ContentControl.Resources>
< ContentControl.Style>
<风格的TargetType =ContentControl中>
< Style.Triggers>
< DataTrigger绑定={结合BoolProperty,的RelativeSource = {的RelativeSource FindAncestor,AncestorType = {X:类型SRC:窗口1}}}VALUE =真>
< setter属性=的ContentTemplateVALUE ={StaticResource的则Template2}/>
< / DataTrigger>
< /Style.Triggers>
< /样式和GT;
< /ContentControl.Style>
< / ContentControl中>
<按钮名称=btnSwitchCONTENT =开关/>
< / StackPanel的>
< /窗GT;
部分类窗口1
公共财产BoolProperty()为布尔
得到
返回的GetValue(BoolPropertyProperty)
到底得的
设置(BYVAL值作为布尔)
的SetValue(BoolPropertyProperty,值)
结束设定
高端物业
公共共享只读BoolPropertyProperty作为的DependencyProperty = DependencyProperty.Register(BoolProperty的GetType(布尔)的GetType(窗口1),新FrameworkPropertyMetadata(假)) 私人小组btnSwitch_Click(BYVAL发件人为System.Object的,BYVAL E上System.Windows.RoutedEventArgs)处理btnSwitch.Click
BoolProperty =不BoolProperty
结束小组
末级
下面的东西,对我的作品:
< ContentControl中CONTENT ={结合SomeBool}>
< ContentControl.Resources>
<的DataTemplate X:键=PinkTemplate>
< TextBlock的文本={结合}后台=粉红/>
< / DataTemplate中>
<的DataTemplate X:键=LimeTemplate>
< TextBlock的文本={结合}后台=石灰/>
< / DataTemplate中>
< /ContentControl.Resources>
< ContentControl.ContentTemplate>
<&DataTemplate的GT;
< ContentControl中名称=CC
CONTENT ={结合}
的ContentTemplate ={StaticResource的PinkTemplate}/>
< DataTemplate.Triggers>
< DataTrigger绑定={结合}VALUE =真>
<二传手的TargetName =CC
属性=的ContentTemplate
VALUE ={StaticResource的LimeTemplate}/>
< / DataTrigger>
< /DataTemplate.Triggers>
< / DataTemplate中>
< /ContentControl.ContentTemplate>
< / ContentControl中>
请注意,我的DataTemplate中是另外一个ContentControl中,这使得我的DataTemplate.Triggers就这一(嵌套)ContentControl中的操作的ContentTemplate
I want the ContentTemplate
to vary according to the value in the DataTrigger
.
And yes, I considered using a DataTemplateSelector
, but now I need a DataTrigger
or better said a MultiDataTrigger
.
Please take a look at the following sample app, the DataTemplate
doesn't change:
<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
Here's something that works for me:
<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>
Note that my DataTemplate is another ContentControl, which allows my DataTemplate.Triggers to operate on the ContentTemplate of that (nested) ContentControl.
这篇关于不能通过DataTrigger设置的ContentTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!