2016-12-27 69 views
0

我有一个包含xaml路径的切换按钮。UWP - 如何将ToggleButton中的Xaml路径绑定到ToggleButton的前景色

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

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

我怎样才能做到这一点>

回答

0

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

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

非常感谢。这可以工作,但我会以这种方式放弃所有的默认动画。我需要做些什么来保持这些动画? –

+0

@ariqyad您可以参考[ToggleButton样式和模板](https://msdn.microsoft.com/en-us/library/windows/apps/mt299157.aspx),并为每个VisualState创建'Storyboard'。 –

0

您需要更改VisualStates在切换按钮的风格。

+0

如何从VisualState引用我的路径? –