繁体   English   中英

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

[英]WPF set a default value for exist property for custom control

我做了一个自定义按钮,可以让我更改背景颜色(将鼠标悬停按下时禁用)

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

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>

正常背景色的默认值-无触发(过分按下-禁用)-是无,我想在设计时将其更改为白色或具有从控件属性更改其功能的功能

如果我正确理解您的要求,您将需要以下内容:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM