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# 当wpf中的按钮有焦点时,更改其背景_C#_Wpf_Button - Fatal编程技术网

C# 当wpf中的按钮有焦点时,更改其背景

C# 当wpf中的按钮有焦点时,更改其背景,c#,wpf,button,C#,Wpf,Button,我有一个按钮,我想改变按钮的背景色。但当我将“背景”属性设置为类似蓝色的颜色时: <Button Width="75" Height="25" Margin="6" Background="Blue"/> 当按钮有焦点时,颜色会变为白色和我的颜色 如何将此白色设置为其他颜色?第一种解决方案是 <Style x:Key="BtnStyle" TargetType="Button"> <Setter Property="FocusVisualSt

我有一个按钮,我想改变按钮的背景色。但当我将“背景”属性设置为类似蓝色的颜色时:

<Button Width="75" Height="25" Margin="6" Background="Blue"/>

当按钮有焦点时,颜色会变为白色和我的颜色

如何将此白色设置为其他颜色?

第一种解决方案是

 <Style x:Key="BtnStyle" TargetType="Button">
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid x:Name="gd" Height="120" Width="120" Background="{TemplateBinding Background}">        
                                <ContentPresenter></ContentPresenter>                            
                    </Grid>
                    <ControlTemplate.Triggers>                                                  
                        <Trigger Property="Button.IsFocused" Value="True">
                            <Setter Property="Background" Value="White" TargetName="gd"></Setter>                           
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Visibility" Value="Collapsed" TargetName="gd"></Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
<Button Width="75" Style="{StaticResource BtnStyle }" Content="ok"  Height="25" Background="Blue"/>

第二种解决方案

      <Style x:Key="Button_focusvisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border SnapsToDevicePixels="True" Background="White">
                        <TextBlock Text="Ok" ></TextBlock>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

      <Button Width="75" FocusVisualStyle="{StaticResource Button_focusvisual }" Content="ok"   Height="25" Background="Blue"/>

我认为您的问题是由
按钮
控件的默认
ControlTemplate
引起的。您描述的彩色动画在此
ControlTemplate
中定义,因此您需要提供自己的
ControlTemplate
来删除此行为:

<Button Content="Click Me" Background="Blue">
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Border Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                <ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" Margin="{TemplateBinding Padding}" />
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="Button.IsFocused" Value="True">
                    <Setter TargetName="Border" Property="Background" Value="White" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Button.Template>
</Button>


检查buttonstate属性。并根据状态添加触发器尝试以下链接中给出的样式。您的代码导致此错误:“在类型“Control”上找不到静态成员“ContentProperty”。“对不起,我还没有回到正确的“工作”模式。”。我忘记设置基本的
TargetType
属性,也完全忘记了
触发器。现在一切都在那里,一切都在进行中。@Sheridan我们如何在全球范围内改变聚焦按钮的样式?我的意思是,如果整个项目中的任何一个按钮被聚焦,就会变成蓝色@Watervilla,您可以在
App.xaml
文件中定义一个全局
样式
,只需省略
x:Key
指令。。。这样,它将应用于应用程序中的所有
按钮。King King已就您的问题提供了一个示例。据报道,自windows 8以来的更改使这一切变得糟糕:/