我正在尝试根据 isChecked 的状态更改用于切换按钮的图标,但是我能找到的有关如何执行此操作的唯一示例使用图像来显示图标。
但是,我有静态图标样式而不是图像,所以我想知道如何做到这一点。
作为起点,如果我像这样创建我的切换按钮:
<ToggleButton Style="{StaticResource ToggleButtonStyle}">
<Path Style="{StaticResource Test1IconStyle}" HorizontalAlignment="Right" />
</ToggleButton>
Run Code Online (Sandbox Code Playgroud)
然后它以 ToggleButton 样式和正确的图标显示。
所以现在假设我单击按钮,我需要根据 isChecked 值将“路径”重置为“Test2IconStyle”。
这可能吗?
我建议您的资源中有两个单独的路径,而不是切换样式,并且根据条件您可以像这样切换它们:
<StackPanel>
<StackPanel.Resources>
<Path x:Key="Test1"/>
<Path x:Key="Test2"/>
</StackPanel.Resources>
<ToggleButton>
<ToggleButton.Style>
<Style TargetType="ToggleButton"
BasedOn="{StaticResource ToggleButtonStyle}">
<Setter Property="Content" Value="{StaticResource Test1}"/>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content"
Value="{StaticResource Test2}"/>
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)