如何绑定到Button的前景色?

时间:2014-11-06 00:23:17

标签: c# wpf xaml windows-phone-8

我的按钮里面有Path元素,我希望它在按下按钮时改变颜色。绑定到前景属性似乎不起作用。简化的代码如下,特别重要的是这一点:Fill =" {Binding Foreground,ElementName = SwitchLanguages}"在路径元素上。我确信有一种简单的方法可以做到这一点,但由于某些原因我无法找到它。

   <Button 
        Name="SwitchLanguages" 
        Background="WhiteSmoke">
        <Canvas
            Width="46.5" 
            Height="44" 
            Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
            <Path 
                Width="46.4999" 
                Height="44" 
                Canvas.Left="0" 
                Canvas.Top="0" 
                Stretch="Fill" 
                Fill="{Binding Foreground, ElementName=SwitchLanguages}"
                Data="F1 M 22,52.0001L 22,44.0001L 46.75,44.0001L 38.75,36L 49.25,36L 61.25,48.0001L 49.25,60L 38.75,60L 46.75,52.0001L 22,52.0001 Z M 54,23.9999L 54,31.9999L 29.25,31.9999L 37.25,40L 26.75,40L 14.75,27.9999L 26.75,16L 37.25,16L 29.25,23.9999L 54,23.9999 Z ">
            </Path>
        </Canvas>
    </Button>

2 个答案:

答案 0 :(得分:1)

如果您检查&#34; System.Windows.xaml&#34;在SDK中,您会看到它实际上是一个模板子项,它在按下按钮时修改了Foreground,而不是控件的Foreground属性本身。

这里理想的方法是修改Button控件的Template。这样你可以把Path放在那里,并根据需要使用&#34; Pressed&#34;修改它的颜色。 VisualState故事板。例如,下面修改了默认ControlTemplate,添加了Path元素,并在&#34; Pressed&#34;时修改了它的Fill属性。视觉状态被触发:

<Style x:Key="MyButtonStyle" TargetType="Button">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="Button">
        <Grid Background="Transparent">

          <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
              <VisualState x:Name="Normal"/>
              <VisualState x:Name="MouseOver"/>
              <VisualState x:Name="Pressed">
                <Storyboard>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentContainer" Storyboard.TargetProperty="Fill">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneBackgroundBrush}" />
                  </ObjectAnimationUsingKeyFrames>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="Background">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}" />
                  </ObjectAnimationUsingKeyFrames>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="BorderBrush">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}" />
                  </ObjectAnimationUsingKeyFrames>
                </Storyboard>
              </VisualState>
              <VisualState x:Name="Disabled">
                <Storyboard>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentContainer" Storyboard.TargetProperty="Fill">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}" />
                  </ObjectAnimationUsingKeyFrames>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="BorderBrush">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}" />
                  </ObjectAnimationUsingKeyFrames>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="Background">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                  </ObjectAnimationUsingKeyFrames>
                </Storyboard>
              </VisualState>
            </VisualStateGroup>
          </VisualStateManager.VisualStateGroups>

          <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="0" Background="{TemplateBinding Background}" Margin="{StaticResource PhoneTouchTargetOverhang}" >

            <Canvas
                Width="46.5" 
                Height="44" 
                Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
                <Path x:Name="ContentContainer"
                    Width="46.4999" 
                    Height="44" 
                    Canvas.Left="0" 
                    Canvas.Top="0" 
                    Stretch="Fill" 
                    Fill="{TemplateBinding Foreground}"
                    Margin="{TemplateBinding Padding}"
                    Data="F1 M 22,52.0001L 22,44.0001L 46.75,44.0001L 38.75,36L 49.25,36L 61.25,48.0001L 49.25,60L 38.75,60L 46.75,52.0001L 22,52.0001 Z M 54,23.9999L 54,31.9999L 29.25,31.9999L 37.25,40L 26.75,40L 14.75,27.9999L 26.75,16L 37.25,16L 29.25,23.9999L 54,23.9999 Z ">
                </Path>
            </Canvas>

          </Border>
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

将上述内容放入您的应用程序资源中,并按照以下方式使用:

答案 1 :(得分:0)

在OnClick事件中,更改按钮的前景色,例如

SwitchLanguages.Foreground = Brushes.Blue;

由于它的绑定,这将改变路径项的绑定颜色。