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

艾哈迈德·穆罕默德(Ahmed Mohammed)

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

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

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>

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

oku

如果我正确理解您的意思,那么您需要这样的东西:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在哪里设置自定义控件默认值

来自分类Dev

C#自定义控件默认值属性

来自分类Dev

如何在下拉菜单控件中为Excel自定义功能区控件设置默认值

来自分类Dev

如何将自定义Controltemplate设置为整个应用程序的默认值?

来自分类Dev

如何将默认值设置为自定义的Google protobuf类型?

来自分类Dev

如何在altair中将自定义颜色主题设置为默认值?

来自分类Dev

使用自定义逻辑为纸张输入容器设置默认值

来自分类Dev

如何将枚举值设置为Web用户控件的自定义属性?

来自分类Dev

为自定义控件设置样式

来自分类Dev

为自定义控件设置样式

来自分类Dev

WPF根据自定义属性的值设置CustomControl样式

来自分类Dev

在每个控件上将事件隧道设置为默认值

来自分类Dev

有什么方法可以在Kendo网格自定义弹出模板中设置默认值?

来自分类Dev

使用QML在Blackberry级联中的自定义选择器中设置默认值

来自分类Dev

在高级搜索中删除自定义属性的默认值

来自分类Dev

如何在目标c中将自定义对象存储为用户默认值?

来自分类Dev

Django自定义管理命令-为参数添加默认值

来自分类Dev

页面加载后,自定义的Woocommerce结帐字段将恢复为默认值

来自分类Dev

pyqt-自定义QComboBox类样式重置为默认值

来自分类Dev

使用OVER的自定义聚合函数,同时又恢复为默认值

来自分类Dev

将 WPF 窗口属性重置为 XAML 中设置的默认值

来自分类Dev

如何在WPF中的FormattedText的简单自定义控件中设置依赖项属性AddOwner

来自分类Dev

使用自定义WPF控件时无法设置某些属性

来自分类Dev

WPF自定义控件不使用ToggleButton的默认样式

来自分类Dev

WPF-自定义控件将填充设置为矩形

来自分类Dev

为什么我的“自定义用户控件属性”值与XAML中设置的值不匹配?

来自分类Dev

使用定义的getter和setter为属性设置默认值

来自分类Dev

从自定义首选项访问默认值

来自分类Dev

自定义标记扩展的默认值?

Related 相关文章

  1. 1

    在哪里设置自定义控件默认值

  2. 2

    C#自定义控件默认值属性

  3. 3

    如何在下拉菜单控件中为Excel自定义功能区控件设置默认值

  4. 4

    如何将自定义Controltemplate设置为整个应用程序的默认值?

  5. 5

    如何将默认值设置为自定义的Google protobuf类型?

  6. 6

    如何在altair中将自定义颜色主题设置为默认值?

  7. 7

    使用自定义逻辑为纸张输入容器设置默认值

  8. 8

    如何将枚举值设置为Web用户控件的自定义属性?

  9. 9

    为自定义控件设置样式

  10. 10

    为自定义控件设置样式

  11. 11

    WPF根据自定义属性的值设置CustomControl样式

  12. 12

    在每个控件上将事件隧道设置为默认值

  13. 13

    有什么方法可以在Kendo网格自定义弹出模板中设置默认值?

  14. 14

    使用QML在Blackberry级联中的自定义选择器中设置默认值

  15. 15

    在高级搜索中删除自定义属性的默认值

  16. 16

    如何在目标c中将自定义对象存储为用户默认值?

  17. 17

    Django自定义管理命令-为参数添加默认值

  18. 18

    页面加载后,自定义的Woocommerce结帐字段将恢复为默认值

  19. 19

    pyqt-自定义QComboBox类样式重置为默认值

  20. 20

    使用OVER的自定义聚合函数,同时又恢复为默认值

  21. 21

    将 WPF 窗口属性重置为 XAML 中设置的默认值

  22. 22

    如何在WPF中的FormattedText的简单自定义控件中设置依赖项属性AddOwner

  23. 23

    使用自定义WPF控件时无法设置某些属性

  24. 24

    WPF自定义控件不使用ToggleButton的默认样式

  25. 25

    WPF-自定义控件将填充设置为矩形

  26. 26

    为什么我的“自定义用户控件属性”值与XAML中设置的值不匹配?

  27. 27

    使用定义的getter和setter为属性设置默认值

  28. 28

    从自定义首选项访问默认值

  29. 29

    自定义标记扩展的默认值?

热门标签

归档