Skip to content

WPF自定义样式属性

约 335 字大约 1 分钟

WPF

2024-10-24

  • 自定义控件属性,使按钮在鼠标移入时改变背景颜色
<Window x:Class="BasisFrame.Views.GlobalMain"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:attr="clr-namespace:BasisFrame.Styling.Attach;assembly=BasisFrame.Styling"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        AllowsTransparency="True">
    <Window.Resources>
        <Style x:Key="MainOperateBtn" TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid Cursor="Hand">
                            <StackPanel x:Name="bg"
                                        Width="{TemplateBinding Width}"
                                        Height="{TemplateBinding Height}"
                                        Background="Transparent" />
                            <ContentPresenter Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" />
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="bg" Property="Opacity" Value="{Binding Path=(attr:BFElement.HoverOpacity), RelativeSource={RelativeSource TemplatedParent}}" />
                                <Setter TargetName="bg" Property="Background" Value="{Binding Path=(attr:BFElement.HoverBackground), RelativeSource={RelativeSource TemplatedParent}}" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <DockPanel Height="50"
                   Margin="0,0,10,0"
                   HorizontalAlignment="Right"
                   VerticalAlignment="Top"
                   Panel.ZIndex="99"
                   ClipToBounds="False"
                   Cursor="">
            <Button x:Name="MinBtn"
                    Width="30"
                    Height="30"
                    attr:BFElement.HoverBackground="Black"
                    attr:BFElement.HoverOpacity=".3"
                    Click="MinBtn_Click"
                    Style="{StaticResource MainOperateBtn}"
                    Visibility="Visible">
                <materialDesign:PackIcon Height="30"
                                         HorizontalAlignment="Center"
                                         Foreground="White"
                                         Kind="WindowMinimize" />
            </Button>

            <Button x:Name="CloseBtn"
                    Width="30"
                    Height="30"
                    attr:BFElement.HoverBackground="Red"
                    attr:BFElement.HoverOpacity="1"
                    Click="CloseBtn_Click"
                    Style="{StaticResource MainOperateBtn}"
                    Visibility="Collapsed">
                <!--  CloseBtn  -->
                <materialDesign:PackIcon Height="30"
                                         HorizontalAlignment="Center"
                                         Foreground="White"
                                         Kind="WindowClose" />
            </Button>
        </DockPanel>
        <ContentControl prism:RegionManager.RegionName="GlobalRegion" />
    </Grid>
</Window>
namespace BasisFrame.Styling.Attach
{
    /// <summary>
    /// basis framework通用控件属性
    /// </summary>
    public class BFElement
    {
        public static readonly DependencyProperty HoverBackgroundProperty = DependencyProperty.RegisterAttached("HoverBackground", typeof(Brush), typeof(BFElement), new FrameworkPropertyMetadata(Brushes.Transparent, FrameworkPropertyMetadataOptions.Inherits));
        public static void SetHoverBackground(DependencyObject element, Brush value) => element.SetValue(HoverBackgroundProperty, value);
        public static Brush GetHoverBackground(DependencyObject element) => (Brush)element.GetValue(HoverBackgroundProperty);


        public static readonly DependencyProperty HoverOpacityProperty = DependencyProperty.RegisterAttached("HoverOpacity", typeof(double), typeof(BFElement), new FrameworkPropertyMetadata(1.0, FrameworkPropertyMetadataOptions.Inherits));
        public static void SetHoverOpacity(DependencyObject element, double value) => element.SetValue(HoverOpacityProperty, value);
        public static double GetHoverOpacity(DependencyObject element) => (double)element.GetValue(HoverOpacityProperty);

    }
}