如何绑定到按钮';s的前景色

本文关键字:前景色 按钮 何绑定 绑定 | 更新日期: 2023-09-27 18:29:22

我的按钮中有Path元素,我希望它在按下按钮时改变颜色。绑定到Foreground属性似乎不起作用。简化的代码如下,特别重要的是这一点: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>

如何绑定到按钮';s的前景色

如果您在SDK中选中"System.Windows.xaml",您会发现它实际上是一个模板子级,在按下按钮时其前景被修改,而不是控件的Foreground属性本身。

这里的理想方法是修改Button控件的Template。这样,您可以将Path放在那里,并根据需要使用"按下"的VisualState情节板修改其颜色。例如,以下修改默认的ControlTemplate,添加Path元素,并在触发"按下"视觉状态时修改其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>

将以上内容放在您的应用程序资源中,并像这样使用:

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

SwitchLanguages.Foreground = Brushes.Blue;

这将由于绑定而更改路径项的绑定颜色。