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
Wpf 更改鼠标上方的背景色_Wpf_Xaml - Fatal编程技术网

Wpf 更改鼠标上方的背景色

Wpf 更改鼠标上方的背景色,wpf,xaml,Wpf,Xaml,我成功地制作了一个圆形边框按钮,但当鼠标移动到上方时,我似乎无法改变它的背景色。不透明度会更改,但背景色不会更改 <Style TargetType="Button" x:Key="FlatButtonStyle"> <Setter Property="OverridesDefaultStyle" Value="True" /> <Setter Property="Cursor" Value="Hand" /> <Setter P

我成功地制作了一个圆形边框按钮,但当鼠标移动到上方时,我似乎无法改变它的背景色。不透明度会更改,但背景色不会更改

<Style TargetType="Button" x:Key="FlatButtonStyle">
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="Cursor" Value="Hand" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Name="border" BorderThickness="0" BorderBrush="Black" Background="{TemplateBinding Background}" CornerRadius="4">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Border>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Orange"/>
            <Setter Property="Opacity" Value="0.91" />

        </Trigger>
    </Style.Triggers>
</Style>

正如你所看到的,我不确定为什么不透明度有效,但另一个不行。但是,我认为这与实际按钮本身存在冲突:

<Button Style="{StaticResource FlatButtonStyle}" Content="Sign In" VerticalAlignment="Top" Margin="10,267,10,0" Background="#FF3650F3" Foreground="White" Height="29" Command="{Binding SignIn}">

有没有办法克服这个问题?我想做的是创建一个通用的圆形按钮模板,将背景更改为橙色。但是我想要设置默认背景的功能,就像我在按钮中显示的那样。

试试这个

<Style TargetType="Button" x:Key="FlatButtonStyle">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type Button}">
            <Border Name="border" BorderThickness="0" BorderBrush="Black" Background="{TemplateBinding Background}" CornerRadius="4">
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
            </Border>
<ControlTemplate.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
        <Setter TargetName="border" Property="Background" Value="Orange"/>
        <Setter Property="Opacity" Value="0.91" />
    </Trigger>
</ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter.Value>
</Setter>


通过其名称直接指向边框,但由于它位于ControlTemplate内部,因此最好将触发器移动到那里。如果你省略了这个名字,比如说不透明度设置器,它的切割器就足以知道你的目标是按钮本身,因为它的控制模板就是那个按钮。因此,您可以针对单个组件以及按钮。

您是否尝试过使用十六进制值,而不是使用
橙色
颜色名称?类似于
#FFFFA500
?@GeoffJames是的,我有,但什么都没有。你看了这个答案吗?:-我想关键部分可能是
,以你的
风格
声明,我刚刚评论了几乎相同的东西-我想关键部分是
?@Dimitri我补充了一些解释,我们总是使用controltemplate触发器,因为这是最简单的方法,而且总是有效的。@Dimitri-所有ControlTemplates都有一个触发器集合,就像样式一样,如果您想为某些东西创建自定义模板,最好将基本功能触发器(如MouseOver/IsPressed等)放在模板本身中。这在样式之上,因为它是决定什么是什么的原始控件模板,并且不能被样式所做的任何事情干扰(如将OverridesDefaultStyle设置为true),除非您告诉样式使用其他(或否)模板。@Logan明白了。谢谢大家的支持!