Наведите курсор на закругленный угол, измените цвет фона, используя ResourceDictionary - PullRequest
0 голосов
/ 02 февраля 2020

Я создал таблицу стилей ResourceDictionary, которая имеет стиль с именем btn_default. Я планирую использовать его на всех кнопках в моей программе. Моя проблема в том, что при наведении на кнопку цвет фона не меняется. Цвет шрифта меняется.

button_default

enter image description here

button_default: hover

enter image description here

Я попытался в своем коде изменить значение "Setter Property =" Background "Value =" # b5b5b5 "", однако я предполагаю, что это не влияет на границу -тег, а стиль-тэг?

ResourceDictionary

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

Main

<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 Ответ

2 голосов
/ 02 февраля 2020

Вы должны объявить Background свойство Border, используя TemplateBinding расширение и установить Background значение в 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...