对 WPF 使用 MVVM 方法,我有一个名为SubButton
. 它的样式如下:
<!-- 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 属性。我收到编译错误,因为编译器无法确定rectBackground
目标的来源。它是 的一部分ControlTemplate
,我不知道该怎么说?关于DataContext
?