如何设置按钮的悬停并单击前景?



我是Styles的新手,不确定创建Button样式以改变IsMouseOverIsPressedForeground的最佳方式。我使用Border作为模板,因为它有CornerRadius和背景和边界属性。但是Border没有Foreground的属性。使用Button作为模板不能很好地工作,因为默认外观没有被覆盖。

Foreground行为设计Button模板的更好方法是什么?也许在边框内使用标签?

App.xaml:

<Application.Resources>
    <Style TargetType="Button" x:Key="MyButton">
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="MinHeight" Value="34"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Name="shortcutbutton"  
                    BorderThickness="1"
                    BorderBrush="Black"
                    Background="Gray">
                        <ContentPresenter Margin="2" 
                                  HorizontalAlignment="Center"
                                  VerticalAlignment="Center" 
                                  RecognizesAccessKey="True"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="shortcutbutton" Property="Background" Value="#FFDADADA" />
                            <Setter TargetName="shortcutbutton" Property="BorderBrush" Value="#EEEEEE" />
                            <!--<Setter TargetName="Border" Property="Foreground" Value="Black" />-->
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter TargetName="shortcutbutton" Property="Background" Value="DarkGray" />
                            <Setter TargetName="shortcutbutton" Property="BorderBrush" Value="Black" />
                            <!--<Setter TargetName="Border" Property="Foreground" Value="Red" />-->
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources>
和xaml:
    <StackPanel HorizontalAlignment="Center" Height="100" VerticalAlignment="Center" Width="100">
        <Button Content="Button" Height="41" Style="{DynamicResource MyButton}"/>
    </StackPanel>

您需要将您的触发器从ControlTemplate移动到样式。像这样:

        <Style TargetType="Button">
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="MinHeight" Value="34"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Name="shortcutbutton"  
                BorderThickness="1"
                BorderBrush="Black"
                Background="Gray">
                        <ContentPresenter Margin="2" 
                              HorizontalAlignment="Center"
                              VerticalAlignment="Center" 
                              RecognizesAccessKey="True"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="Foreground" Value="#FFDADADA" />
            </Trigger>
            <Trigger Property="IsPressed" Value="true">
                <Setter Property="Foreground" Value="DarkGray" />
            </Trigger>
        </Style.Triggers>
    </Style>

你可以做的是将TextElement的附加属性设置在Border上,像Border中的每个文本元素都将使用该颜色:

<ControlTemplate.Triggers>
   <Trigger Property="IsMouseOver" Value="true">
       <Setter TargetName="shortcutbutton" Property="Background" Value="#FFDADADA" />
       <Setter TargetName="shortcutbutton" Property="BorderBrush" Value="#EEEEEE" />
       <Setter TargetName="shortcutbutton" Property="TextElement.Foreground" Value="White"/>
   </Trigger>
   ...
</ControlTemplate.Triggers>

相关内容

  • 没有找到相关文章

最新更新