Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何绑定到按钮';什么是前景色?_C#_Wpf_Xaml_Windows Phone 8 - Fatal编程技术网

C# 如何绑定到按钮';什么是前景色?

C# 如何绑定到按钮';什么是前景色?,c#,wpf,xaml,windows-phone-8,C#,Wpf,Xaml,Windows Phone 8,我有路径元素在我的按钮,我希望它改变颜色时,按钮被按下。绑定到前台属性似乎不起作用。下面是简化的代码,尤其重要的是这个位:Path元素上的Fill=“{Binding Foreground,ElementName=SwitchLanguages}”。我相信有一个简单的方法可以做到这一点,但由于某种原因,我无法找到它 <Button Name="SwitchLanguages" Background="WhiteSmoke">

我有路径元素在我的按钮,我希望它改变颜色时,按钮被按下。绑定到前台属性似乎不起作用。下面是简化的代码,尤其重要的是这个位: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>

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

SwitchLanguages.Foreground = Brushes.Blue;
这将由于路径项的绑定而更改其绑定的颜色。

如果您在SDK中选中“System.Windows.xaml”,您将看到它实际上是一个模板子项,当按下按钮时,它的前景被修改,而不是控件的
Foreground
属性本身

这里的理想方法是修改按钮控件的
模板
。这样,您就可以将
路径
放在那里,并使用“按下的”
VisualState
情节提要根据需要修改其颜色。例如,下面修改默认的
控制模板
,添加
路径
元素,并在触发“按下”视觉状态时修改其
填充
属性:

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

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


单击时开关语言的颜色是否会改变?如果您没有做一些特殊的操作,则不会更改。按下时颜色确实会更改(不是控件的
前台属性,而是其内容的颜色)--这是基于按钮的默认模板。这不是最佳解决方案--您需要添加另一个事件处理程序以切换回颜色。最好是在ControlTemplate中连接到“按下”的VisualState。@McGarnagle我的目的只是为正在学习Xaml开发的人提供一个简单的解决方案。不多不少。如果目的证明了方法的正确性,我就让最终用户来决定。@OmegaMan我知道我可以尝试更改代码中的颜色,但我确信必须有更好的方法来做到这一点。