UWP - 如何将 ToggleButton 中的 Xaml 路径绑定到 ToggleButton 的前景色

UWP - How to bind a Xaml Path within ToggleButton to the ToggleButton's Foreground Color

我有一个包含 xaml 路径的切换按钮。

我想将路径的填充颜色设置为切换按钮的前景色,以便当按钮未按下时为黑色,按下时为白色。

        <ToggleButton>
            <Path Data="M5.08333,4.5 L10.8333,4.33333 L17.3749,8.24242 L6.97926,10.6125 C6.97926,10.6125 7.35392,5.18685 5.08333,4.5 z" Fill="#FFF4F4F5" Height="6.291" Stretch="Fill" UseLayoutRounding="False" Width="12.292"/>
        </ToggleButton>

我该怎么做>

您需要更改 ToggleButton 样式中的 VisualStates。

您可以创建一个 ControlTemplate for the ToggleButton,然后在每个 VisualState 中为 shapes 配置 Fill 属性。

<Page.Resources>
    <ControlTemplate x:Key="ToggleButtonWithShapes" TargetType="ToggleButton">
        <Border
            Background="{TemplateBinding Background}"
            BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}">
            <Grid>
                <Path
                    x:Name="shapes"
                    Width="60"
                    Height="20"
                    Data="M5.08333,4.5 L10.8333,4.33333 L17.3749,8.24242 L6.97926,10.6125 C6.97926,10.6125 7.35392,5.18685 5.08333,4.5 z"
                    Fill="Black"
                    Stretch="Fill"
                    UseLayoutRounding="True" />
            </Grid>
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="Pressed">
                        <VisualState.Setters>
                            <Setter Target="shapes.Fill" Value="White" />
                        </VisualState.Setters>
                    </VisualState>
                    <VisualState x:Name="CheckedPressed">
                        <VisualState.Setters>
                            <Setter Target="shapes.Fill" Value="Black" />
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
        </Border>
    </ControlTemplate>
</Page.Resources>