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

时间:2016-12-27 07:12:01

标签: uwp uwp-xaml

我有一个包含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>

我该怎么做&gt;

2 个答案:

答案 0 :(得分:0)

您需要以ToggleButton的风格更改VisualStates。

答案 1 :(得分:0)

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

<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>