WPF:从DataTemplate修改ControlTemplate属性?
问题描述:
使用WPF的MVVM方法,我有一个名为SubButton
的视图模型类。WPF:从DataTemplate修改ControlTemplate属性?
<!-- SubNavButton Appearance -->
<ControlTemplate x:Key="SubNavButton" TargetType="{x:Type RadioButton}">
<Grid Margin="5,5">
<Rectangle x:Name="rectBackground" Fill="DimGray" Stroke="#FF000000" Width="150" Height="40" StrokeThickness="1"/>
<TextBlock x:Name="txtContent" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Arial" FontSize="16">
<ContentPresenter />
</TextBlock>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Fill" TargetName="rectBackground" Value="Red"/>
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Fill" TargetName="rectBackground" Value="Pink"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<!-- SubButton data template -->
<DataTemplate DataType="{x:Type VM:SubButton}">
<RadioButton
Template="{StaticResource SubNavButton}"
Content="{Binding TempText}"
Command="{Binding SubFrameChanger.Command}"
CommandParameter="{Binding Key}"
GroupName="{Binding MenuGroup}"
V:CreateCommandBinding.Command="{Binding SubFrameChanger}" />
<DataTemplate.Triggers>
<!-- This trigger doesn't work -->
<DataTrigger Binding="{Binding Path=Selected}" Value="True">
<Setter TargetName="rectBackground" Property="Fill" Value="Green"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
的DataTrigger
不起作用:它如下的样式。 Selected
是SubButton
类中的常规.Net属性。我收到编译错误,因为编译器无法确定目标是从哪里来的。它是ControlTemplate
的一部分,我不确定如何告诉它?关于DataContext
的一些事情?
答
你想要什么是不可能的。 WPF与NameScopes协同工作,名称rectBackground超出了DataTemplate中的范围。原始名称rectBackground只会在原始ControlTemplate中的范围内。这很幸运,因为否则你将无法在整个应用程序中使用重名。 你可以做的是通过TemplateBinding将rectBackground的Fill属性绑定到RadioButton的Background属性。当你在你的代码的其他地方改变RadioButton的背景时,rectBackground将把这个Brush作为它的填充。为了说明这一点,我修改了一下代码。使用DataTemplate将其更改为模型将很容易。
<Window.Resources>
<ControlTemplate x:Key="SubNavButton" TargetType="RadioButton">
<Grid Margin="5,5">
<Rectangle x:Name="rectBackground"
Fill="{TemplateBinding Background}"
Stroke="#FF000000" Width="150" Height="40"
StrokeThickness="1"/>
<TextBlock x:Name="txtContent" HorizontalAlignment="Center"
VerticalAlignment="Center"
FontFamily="Arial" FontSize="16">
<ContentPresenter />
</TextBlock>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Fill"
TargetName="rectBackground" Value="Red"/>
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Fill"
TargetName="rectBackground" Value="Pink"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<!-- The Style simulates the same behavior as the DataTemplate-->
<Style TargetType="RadioButton">
<Setter Property="Template"
Value="{StaticResource SubNavButton}"/>
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="Green"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<RadioButton>one</RadioButton>
<RadioButton>two</RadioButton>
</StackPanel>
谢谢,TemplateBinding使得很多更有意义,我不知道如何“外面的世界”的东西都不能进入控制:) 然而,DataTrigger仍然还没有工作... – evilfred 2009-11-29 18:39:24
我觉得这未设定正确器isChecked(应chnage的背景颜色): DataTrigger> DataTemplate> –
evilfred
2009-11-29 18:40:02
这是在我的例子中工作:只需更改样式触发器中的Setter。问题必须在绑定的Selected属性中。它是否正确实施更改通知? – Dabblernl 2009-11-29 18:50:24