代码之家  ›  专栏  ›  技术社区  ›  Europa

C#Wpf悬停圆角按钮使用ResourceDictionary更改其背景色

wpf
  •  0
  • Europa  · 技术社区  · 5 年前

    我打算把它用在我程序的所有按钮上。我的问题是,当我悬停按钮时,背景色不会改变。字体颜色会改变。

    默认按钮

    enter image description here

    enter image description here

    我试图在我的代码中更改“Setter Property=”Background“Value=”b5b5b5“,但是我猜这不会影响Border标记,但是样式标记?

    字典资源

    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:MSDNSample">
    
        <!-- Btn default -->
        <Style x:Key="btn_default" TargetType="{x:Type Button}">
            <Setter Property="FontFamily" Value="Calibri"/>
            <Setter Property="FontSize" Value="14"/>
            <Setter Property="Foreground" Value="#d9d9d9" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border CornerRadius="5" BorderThickness="1" Padding="6,4,6,4" Background="#6c6c6c" BorderBrush="#393939">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="#b5b5b5"/>
                                <Setter Property="Foreground" Value="#000000" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
    
                </Setter.Value>
            </Setter>
        </Style>
        <!-- //Btn default -->
    </ResourceDictionary>
    

    <Button x:Name="buttonExplorer" Content="Explorer" Style="{StaticResource btn_default}" Margin="0,0,6,0" />
    <Button x:Name="buttonProcess" Content="Process" Style="{StaticResource btn_default}" Margin="0,0,6,0" />
    
    1 回复  |  直到 5 年前
        1
  •  1
  •   Pavel Anikhouski    5 年前

    你应该申报 Background 财产 Border 使用 TemplateBinding 背景 价值 Style

    <Style x:Key="btn_default" TargetType="{x:Type Button}">
       <Setter Property="FontFamily" Value="Calibri"/>
       <Setter Property="FontSize" Value="14"/>
       <Setter Property="Foreground" Value="#d9d9d9" />
       <Setter Property="Background" Value="#6c6c6c"/>
       <Setter Property="Template">
          <Setter.Value>
               <ControlTemplate TargetType="Button">
                   <Border CornerRadius="5" BorderThickness="1" Padding="6,4,6,4" BorderBrush="#393939" Background="{TemplateBinding Background}">
                         <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                   </Border>
                   <ControlTemplate.Triggers>
                       <Trigger Property="IsMouseOver" Value="True">
                           <Setter Property="Background" Value="#b5b5b5"/>
                           <Setter Property="Foreground" Value="#000000" />
                       </Trigger>
                   </ControlTemplate.Triggers>
               </ControlTemplate>
            </Setter.Value>
       </Setter>
    </Style>