Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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_Xaml_Button_Styles - Fatal编程技术网

C# 如何设置鼠标在WPF中悬停时更改颜色的无边框按钮

C# 如何设置鼠标在WPF中悬停时更改颜色的无边框按钮,c#,wpf,xaml,button,styles,C#,Wpf,Xaml,Button,Styles,我想将所有按钮的样式替换为一个无边框的灰色按钮,当鼠标悬停在该按钮上时,该按钮将高亮显示 我写的如下: 如果我删除模板部分,我甚至不知道它会做什么,即使我已将BorderThickness设置为0,按钮也会有边框。 但是如果我保留模板部分,按钮将根本不会更改其背景颜色 那么我该怎么做才能保留这两个特性呢?为什么我的xaml不能工作 顺便说一句,我在哪里可以找到我可以为某些类型的控件(如按钮)设置的属性/触发器的完整列表 <Style TargetType="{x:Type Butt

我想将所有按钮的样式替换为一个无边框的灰色按钮,当鼠标悬停在该按钮上时,该按钮将高亮显示

我写的如下: 如果我删除模板部分,我甚至不知道它会做什么,即使我已将BorderThickness设置为0,按钮也会有边框。 但是如果我保留模板部分,按钮将根本不会更改其背景颜色

那么我该怎么做才能保留这两个特性呢?为什么我的xaml不能工作

顺便说一句,我在哪里可以找到我可以为某些类型的控件(如按钮)设置的属性/触发器的完整列表

    <Style TargetType="{x:Type Button}">
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="Background" Value="{StaticResource TitleBrush}" />
        <Setter Property="Foreground" Value="{StaticResource WhiteTextBrush}"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <ContentPresenter Content="{TemplateBinding Content}"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="{StaticResource HoverBrush}"/>
            </Trigger>
        </Style.Triggers>
    </Style>

试试这样的,会有用的

<Grid>
    <Grid.Resources>
        <ControlTemplate x:Key="buttonTemplate" TargetType="{x:Type Button}">
            <Grid>
                <Rectangle x:Name="Rect" Width="100" Height="100" Fill="Aqua"/>

                <Viewbox>
                    <ContentControl Margin="20" Content="{TemplateBinding Content}"/>
                </Viewbox>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="Rect" Property="Fill" Value="Orange"/>
                </Trigger>

            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Grid.Resources>
    <Button Template="{StaticResource buttonTemplate}">OK</Button>
</Grid>
为什么边界厚度值未反映到控件

为什么你的xaml不能工作

因为,您使用简单的内容演示器设置了按钮的模板属性,所以按钮忽略所有控件属性并反映您的模板。一种方法是使用“按示例”标签改进模板属性:

<ControlTemplate TargetType="{x:Type Button}">
    <Label Content="{TemplateBinding Content}"  
        Background="{TemplateBinding Background}" 
        Foreground="{TemplateBinding Foreground}" 
        BorderThickness="{TemplateBinding BorderThickness}" 
        BorderBrush="Black"/>
</ControlTemplate>

我得到了我想要的答案。但我还是想知道为什么。。。