我遇到一个问题,即在使用“路径图形”对象时,按下ToggleButton并没有理想的效果。在执行DataTrigger时,它始终与原始路径保持一致。
我的XAML:
<Window x:Class="Scratch.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Scratch"
xmlns:vm="clr-namespace:Scratch.ViewModels"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance vm:ToggleViewModel, IsDesignTimeCreatable=True}"
Title="Toggle Me!!!!" Height="110" Width="300" Loaded="MainWindowOnLoaded">
<Grid x:Name="GridLayout" DataContext="{Binding}">
<Grid.Resources>
<Style x:Key="PathGraphicStyle" TargetType="{x:Type Path}">
<Setter Property="Fill" Value="CornflowerBlue"/>
<Setter Property="Stretch" Value="Uniform"/>
<Setter Property="UseLayoutRounding" Value="True"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
</Style>
<Style x:Key="PathGraphicOrangeRedStyle" TargetType="{x:Type Path}">
<Setter Property="Fill" Value="OrangeRed"/>
<Setter Property="Stretch" Value="Uniform"/>
<Setter Property="UseLayoutRounding" Value="True"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
</Style>
<Style x:Key="PathGraphicGreenStyle" TargetType="{x:Type Path}">
<Setter Property="Fill" Value="ForestGreen"/>
<Setter Property="Stretch" Value="Uniform"/>
<Setter Property="UseLayoutRounding" Value="True"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
</Style>
<Style x:Key="PathGraphicYellowStyle" TargetType="{x:Type Path}">
<Setter Property="Fill" Value="Gold"/>
<Setter Property="Stretch" Value="Uniform"/>
<Setter Property="UseLayoutRounding" Value="True"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
</Style>
<Path x:Shared="false" x:Key="RefreshGraphic" Style="{StaticResource PathGraphicStyle}" Data="F1 M 20.16,18.4264C 20.1046,20.6826 19.3332,22.5612 17.8459,24.0623C 16.3586,25.5634 14.4879,26.3426 12.2336,26.4C 9.97745,26.3426 8.09882,25.5634 6.59772,24.0623C 5.09662,22.5612 4.31738,20.6825 4.26001,18.4264C 4.31537,16.1964 5.08658,14.3378 6.57355,12.8507C 8.06058,11.3636 9.93115,10.58 12.1853,10.5L 11.0739,17.6048L 23.2842,8.32254L 10.5906,3.05176e-005L 12.137,6.24002C 10.5067,6.24692 8.95892,6.55258 7.49365,7.15701C 6.02832,7.76147 4.71387,8.62335 3.55023,9.74265C 1.23074,12.1707 0.0473022,15.0653 0,18.4264C 0.00592041,20.0818 0.313599,21.6518 0.923035,23.1362C 1.5325,24.6206 2.40823,25.9451 3.55023,27.1098C 4.71487,28.2725 6.03937,29.1541 7.52383,29.7547C 9.00824,30.3553 10.5782,30.6571 12.2336,30.66C 13.8891,30.6571 15.459,30.3553 16.9435,29.7547C 18.428,29.1541 19.7526,28.2725 20.9174,27.1098C 22.0574,25.9451 22.9252,24.6206 23.5208,23.1362C 24.1163,21.6518 24.416,20.0818 24.42,18.4264L 20.16,18.4264 Z " />
<Path x:Shared="false" x:Key="PlusCircle" Style="{StaticResource PathGraphicStyle}" Data="F1 M 2.88,17.2725C 0.959991,15.3475 0,12.95 0,10.08C 0,7.21002 0.959991,4.8125 2.88,2.88751C 4.80002,0.962494 7.19125,0 10.0537,0C 12.9163,0 15.3163,0.962494 17.2538,2.88751C 19.1913,4.8125 20.16,7.21002 20.16,10.08C 20.16,12.95 19.1913,15.3475 17.2538,17.2725C 15.3163,19.1975 12.9163,20.16 10.0537,20.16C 7.19125,20.16 4.80002,19.1975 2.88,17.2725 Z M 3.84,11.3625L 8.81998,11.3625L 8.81998,16.32L 11.34,16.32L 11.34,11.3625L 16.32,11.3625L 16.32,8.85001L 11.34,8.85001L 11.34,3.84L 8.81998,3.84L 8.81998,8.85001L 3.84,8.85001L 3.84,11.3625 Z "/>
<Path x:Shared="false" x:Key="PlusThreeCircle" Style="{StaticResource PathGraphicOrangeRedStyle}" Data="F1 M 2.88,17.2725C 0.959991,15.3475 0,12.95 0,10.08C 0,7.21002 0.959991,4.8125 2.88,2.88751C 4.80002,0.962494 7.19125,0 10.0537,0C 12.9163,0 15.3163,0.962494 17.2538,2.88751C 19.1913,4.8125 20.16,7.21002 20.16,10.08C 20.16,12.95 19.1913,15.3475 17.2538,17.2725C 15.3163,19.1975 12.9163,20.16 10.0537,20.16C 7.19125,20.16 4.80002,19.1975 2.88,17.2725 Z M 3.84,11.3625L 8.81998,11.3625L 8.81998,16.32L 11.34,16.32L 11.34,11.3625L 16.32,11.3625L 16.32,8.85001L 11.34,8.85001L 11.34,3.84L 8.81998,3.84L 8.81998,8.85001L 3.84,8.85001L 3.84,11.3625 Z "/>
<Path x:Shared="false" x:Key="NextDayProfitLossCircle" Style="{StaticResource PathGraphicGreenStyle}" Data="F1 M 2.88,17.2725C 0.959991,15.3475 0,12.95 0,10.08C 0,7.21002 0.959991,4.8125 2.88,2.88751C 4.80002,0.962494 7.19125,0 10.0537,0C 12.9163,0 15.3163,0.962494 17.2538,2.88751C 19.1913,4.8125 20.16,7.21002 20.16,10.08C 20.16,12.95 19.1913,15.3475 17.2538,17.2725C 15.3163,19.1975 12.9163,20.16 10.0537,20.16C 7.19125,20.16 4.80002,19.1975 2.88,17.2725 Z M 3.84,11.3625L 8.81998,11.3625L 8.81998,16.32L 11.34,16.32L 11.34,11.3625L 16.32,11.3625L 16.32,8.85001L 11.34,8.85001L 11.34,3.84L 8.81998,3.84L 8.81998,8.85001L 3.84,8.85001L 3.84,11.3625 Z "/>
<Path x:Shared="false" x:Key="CurrentDayProfitLossCircle" Style="{StaticResource PathGraphicYellowStyle}" Data="F1 M 2.88,17.2725C 0.959991,15.3475 0,12.95 0,10.08C 0,7.21002 0.959991,4.8125 2.88,2.88751C 4.80002,0.962494 7.19125,0 10.0537,0C 12.9163,0 15.3163,0.962494 17.2538,2.88751C 19.1913,4.8125 20.16,7.21002 20.16,10.08C 20.16,12.95 19.1913,15.3475 17.2538,17.2725C 15.3163,19.1975 12.9163,20.16 10.0537,20.16C 7.19125,20.16 4.80002,19.1975 2.88,17.2725 Z M 3.84,11.3625L 8.81998,11.3625L 8.81998,16.32L 11.34,16.32L 11.34,11.3625L 16.32,11.3625L 16.32,8.85001L 11.34,8.85001L 11.34,3.84L 8.81998,3.84L 8.81998,8.85001L 3.84,8.85001L 3.84,11.3625 Z "/>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ToggleButton Grid.Row="0" Content="{StaticResource PlusCircle}"/>
<ToggleButton Grid.Row="1" Content="{StaticResource PlusThreeCircle}"/>
<ToggleButton x:Name="ToggleButtonProfitLoss" Grid.Row="2" Content="{DynamicResource NextDayProfitLossCircle}" IsChecked="{Binding NextDayProfitLoss}">
<ToggleButton.Style>
<!--<Style TargetType="ToggleButton" BasedOn="{StaticResource {x:Static ToolBar.ToggleButtonStyleKey}}">-->
<Style TargetType="ToggleButton">
<Setter Property="ToolTip" Value="Show Next Day PL" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=ToggleButtonProfitLoss, Path=IsChecked}" Value="False">
<Setter Property="ToolTip" Value="Show Next Day PL" />
<Setter Property="Content" Value="{DynamicResource NextDayProfitLossCircle}"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=ToggleButtonProfitLoss, Path=IsChecked}" Value="True">
<Setter Property="ToolTip" Value="Show CurrentDay PL" />
<Setter Property="Content" Value="{DynamicResource CurrentDayProfitLossCircle}"/>
<Setter Property="Background" Value="DarkKhaki"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
</Grid>
</Window>
还有我的ViewModel类:
namespace Scratch.ViewModels
{
public class ToggleViewModel : Scratch.UiUtilities.PropertyChangedNotifier
{
private bool _nextDayProfitLoss;
public bool NextDayProfitLoss
{
get => _nextDayProfitLoss;
set
{
_nextDayProfitLoss = value;
OnPropertyChanged();
}
}
}
}
调试时,在Visual Studio的“输出”窗口中没有出现任何错误。我应该使用另一种方法吗?我应该改用路径图片吗?我希望尽可能使用所有XAML。