我想要做的就是这样,当用户点击“复选框”时,它会切换样式,使其显示如下。正如您在下图所示,它从灰色的心脏变为红色的心脏。所有绑定和向量都在下面的代码中。我只是不确定如何使用我的自定义路径设置复选框的样式,并设置样式触发器。希望有人可以帮助我。
XAML
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:self="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="300"
WindowStartupLocation="CenterScreen">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Content="Print" Click="Button_Click" Grid.Row="0"/>
<TreeView x:Name="trvFamilies" Grid.Row="1">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type self:Family}" ItemsSource="{Binding Members}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
<TextBlock Text=" [" Foreground="Blue" />
<TextBlock Text="{Binding Members.Count}" Foreground="Blue" />
<TextBlock Text="]" Foreground="Blue" />
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type self:FamilyMember}">
<StackPanel Orientation="Horizontal">
<Grid Margin="0,0,10,0">
<Path SnapsToDevicePixels="True" Stretch="Uniform" Width="12" Fill="Transparent" Stroke="DarkGray" StrokeThickness="2" Data="M0.5,0.5L10.5,0.5 15.25,5.75 15.25,20.5 0.5,20.5z">
</Path>
<Path SnapsToDevicePixels="True" HorizontalAlignment="Center" Stretch="Uniform" Width="14" Fill="Red" Data="M4.833,2.16655086864422C4.833,3.36307577915264 3.66700013383223,4.24925064372868 2.6665,4.833 1.75033338617037,4.16590547819802 0.5,3.36307577915264 0.5,2.16655086864422 0.5,0.970025958135794 2.25033340729955,0.748849667983149 2.6665,2.16665418486839 3.08366677584818,0.832053199886277 4.833,0.970025958135794 4.833,2.16655086864422z">
<Path.Effect>
<DropShadowEffect BlurRadius="2" Opacity="0.5" ShadowDepth="2" />
</Path.Effect>
</Path>
</Grid>
<CheckBox Margin="0,0,10,4" IsChecked="{Binding Favorite}"></CheckBox>
<TextBlock Text="{Binding Name}" />
<TextBlock Text=" (" Foreground="Green" />
<TextBlock Text="{Binding Age}" Foreground="Green" />
<TextBlock Text=" years)" Foreground="Green" />
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>
</Window>