WPF为自定义控件的exist属性设置了默认值

本文关键字:设置 默认值 属性 exist 自定义控件 WPF | 更新日期: 2023-09-27 17:58:19

我制作了一个自定义按钮,可以让我更改背景颜色正常悬停按下禁用

我为所有的属性都做了一个新的属性,除了正常值之外都有默认值,因为它已经存在了,但我不知道如何为它设置默认值

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace ProPharmacyManagerW.Controls
{
    class IconButton : Button
    {
        public static readonly DependencyProperty ImageProperty = DependencyProperty.Register("Image", typeof(ImageSource), typeof(IconButton), new PropertyMetadata(null));
        public static readonly DependencyProperty ImageHeightProperty = DependencyProperty.Register("ImageHeight", typeof(double), typeof(IconButton), new PropertyMetadata(double.NaN));
        public static readonly DependencyProperty ImageWidthProperty = DependencyProperty.Register("ImageWidth", typeof(double), typeof(IconButton), new PropertyMetadata(double.NaN));
        public static readonly DependencyProperty HoverColorProperty = DependencyProperty.Register("ColorHover", typeof(SolidColorBrush), typeof(IconButton), new PropertyMetadata(Brushes.LightGray));
        public static readonly DependencyProperty PressedColorProperty = DependencyProperty.Register("ColorPressed", typeof(SolidColorBrush), typeof(IconButton), new PropertyMetadata(Brushes.Gray));
        public static readonly DependencyProperty DisabledColorProperty = DependencyProperty.Register("ColorDisabled", typeof(SolidColorBrush), typeof(IconButton), new PropertyMetadata(Brushes.Gray));
        static IconButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(IconButton), new FrameworkPropertyMetadata(typeof(IconButton)));
        }
        public ImageSource Image
        {
            get { return (ImageSource)GetValue(ImageProperty); }
            set { SetValue(ImageProperty, value); }
        }
        public double ImageHeight
        {
            get { return (double)GetValue(ImageHeightProperty); }
            set { SetValue(ImageHeightProperty, value); }
        }
        public double ImageWidth
        {
            get { return (double)GetValue(ImageWidthProperty); }
            set { SetValue(ImageWidthProperty, value); }
        }
        public SolidColorBrush ColorHover
        {
            get { return (SolidColorBrush)GetValue(HoverColorProperty); }
            set { SetValue(HoverColorProperty, value); }
        }
        public SolidColorBrush ColorPressed
        {
            get { return (SolidColorBrush)GetValue(PressedColorProperty); }
            set { SetValue(PressedColorProperty, value); }
        }
        public SolidColorBrush ColorDisabled
        {
            get { return (SolidColorBrush)GetValue(DisabledColorProperty); }
            set { SetValue(DisabledColorProperty, value); }
        }
    }
}

主题

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:ProPharmacyManagerW"
                    xmlns:f="clr-namespace:ProPharmacyManagerW.Controls"
                    xmlns:vm="clr-namespace:ProPharmacyManagerW.ViewModel">
    <vm:VisibilityConvertor x:Key="VisibilityConvertor"/>
    <Style TargetType="{x:Type f:IconButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type f:IconButton}">
                    <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}">
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                            <Image x:Name="Buttonicon" 
                                   Margin="10 0"
                                   Source="{TemplateBinding Image}"
                                   Width="{TemplateBinding ImageWidth}"
                                   Height="{TemplateBinding ImageHeight}"
                                   Visibility="{TemplateBinding Image,Converter={StaticResource VisibilityConvertor}}"
                                   HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                   VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                   Grid.Column="0"/>
                            <TextBlock x:Name="contentText"
                                       Text="{TemplateBinding Content}"
                                       HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                       Margin="{TemplateBinding Padding}" 
                                       VerticalAlignment="Center"
                                       Grid.Column="1"/>
                        </StackPanel>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="Background" TargetName="border" Value="{Binding ColorHover, RelativeSource={RelativeSource TemplatedParent}}" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter Property="Background" TargetName="border" Value="{Binding ColorPressed, RelativeSource={RelativeSource TemplatedParent}}" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Background" TargetName="border" Value="{Binding ColorDisabled, RelativeSource={RelativeSource TemplatedParent}}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

正常背景颜色的默认值-没有触发器(过度按压-禁用)-是none,我想将其更改为白色或在设计时可以从控件属性更改的东西

WPF为自定义控件的exist属性设置了默认值

如果我理解正确,你想要这样的东西:

  <Style TargetType="{x:Type f:IconButton}">
<!-- Here the default Override-->
                <Setter Property="Background" Value="DeepPink"/></Setter>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type f:IconButton}">
                            <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}">
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                                    <Image x:Name="Buttonicon" 
                                       Margin="10 0"
                                       Source="{TemplateBinding Image}"
                                       Width="{TemplateBinding ImageWidth}"
                                       Height="{TemplateBinding ImageHeight}"
                                       Visibility="{TemplateBinding Image,Converter={StaticResource VisibilityConvertor}}"
                                       HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                       VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                       Grid.Column="0"/>
                                    <TextBlock x:Name="contentText"
                                           Text="{TemplateBinding Content}"
                                           HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                           Margin="{TemplateBinding Padding}" 
                                           VerticalAlignment="Center"
                                           Grid.Column="1"/>
                                </StackPanel>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="true">
                                    <Setter Property="Background" TargetName="border" Value="{Binding ColorHover, RelativeSource={RelativeSource TemplatedParent}}" />
                                </Trigger>
                                <Trigger Property="IsPressed" Value="true">
                                    <Setter Property="Background" TargetName="border" Value="{Binding ColorPressed, RelativeSource={RelativeSource TemplatedParent}}" />
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="false">
                                    <Setter Property="Background" TargetName="border" Value="{Binding ColorDisabled, RelativeSource={RelativeSource TemplatedParent}}" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>