WPF中依赖属性及附加属性的概念及用法

完全来源于十月的寒流,感谢大佬讲解

依赖属性

由依赖属性提供的属性功能

与字段支持的属性不同,依赖属性扩展了属性的功能。 通常,添加的功能表示或支持以下功能之一:

  • 资源
  • 数据绑定
  • 样式
  • 动画
  • 元数据重写
  • 属性值继承
  • WPF 设计器集成
csharp 复制代码
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            CustomTextBox customTextBox = new CustomTextBox();
            customTextBox.IsHightLighted = true;

            customTextBox.SetValue(CustomTextBox.IsHightLightedProperty, true);
        }
    }

    public class CustomTextBox : TextBox
    {
        public bool IsHightLighted
        {
            get { return (bool)GetValue(IsHightLightedProperty); }
            set { SetValue(IsHightLightedProperty, value); }
        }

        public static readonly DependencyProperty IsHightLightedProperty =
            DependencyProperty.Register("IsHightLighted", typeof(bool), typeof(CustomTextBox), new PropertyMetadata(false));
    }
csharp 复制代码
#region HasText
public bool HasText => (bool)GetValue(HasTextProperty);

public static readonly DependencyProperty HasTextProperty;
public static readonly DependencyPropertyKey HasTextPropertyKey;

static CustomTextBox()
{
    HasTextPropertyKey = DependencyProperty.RegisterReadOnly(
        "HasText",
        typeof(bool),
        typeof(CustomTextBox),
        new PropertyMetadata(false)
        );
    HasTextProperty = HasTextPropertyKey.DependencyProperty;
}
#endregion
csharp 复制代码
<Window x:Class="Test_05.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Test_05"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <Window.Resources>
        <Style TargetType="local:CustomTextBox">
            <Style.Triggers>
                <Trigger Property="IsHightLighted" Value="True">
                    <Setter Property="Background" Value="Yellow"></Setter>
                </Trigger>
                <Trigger Property="IsHightLighted" Value="False">
                    <Setter Property="Background" Value="Blue"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <StackPanel>
        <local:CustomTextBox Text="Hello World!" FontSize="30" IsHightLighted="True"></local:CustomTextBox>
    </StackPanel>
</Window>

附加属性

csharp 复制代码
    <StackPanel>
        <!--<local:CustomTextBox Text="Hello World!" FontSize="30" IsHightLighted="True"></local:CustomTextBox>-->
        <local:CustomTextBox Text="Hello World!" FontSize="30">
            <local:CustomTextBox.IsHightLighted>
                true
            </local:CustomTextBox.IsHightLighted>
            <Grid.Row>1</Grid.Row>
        </local:CustomTextBox>
    </StackPanel>
<StackPanel>
    <local:CustomTextBox x:Name="aoteman" local:TextBoxHelper.Title="this is test" FontSize="30"
                         Text="{Binding RelativeSource={RelativeSource self}, Path=(local:TextBoxHelper.Title)}"></local:CustomTextBox>

    <!--<local:CustomTextBox x:Name="aoteman" FontSize="30"
                         Text="{Binding RelativeSource={RelativeSource self}, Path=(local:TextBoxHelper.Title)}"></local:CustomTextBox>-->
</StackPanel>

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        //TextBoxHelper.SetTitle(aoteman, "this is aoteman");
    }
}

public class CustomTextBox : TextBox
{
    public bool IsHightLighted
    {
        get { return (bool)GetValue(IsHightLightedProperty); }
        set { SetValue(IsHightLightedProperty, value); }
    }

    public static readonly DependencyProperty IsHightLightedProperty =
        DependencyProperty.Register("IsHightLighted", typeof(bool), typeof(CustomTextBox), new PropertyMetadata(false));

    public bool HasText => (bool)GetValue(HasTextProperty);

    public static readonly DependencyProperty HasTextProperty;
    public static readonly DependencyPropertyKey HasTextPropertyKey;

    static CustomTextBox()
    {
        HasTextPropertyKey = DependencyProperty.RegisterReadOnly(
            "HasText",
            typeof(bool),
            typeof(CustomTextBox),
            new PropertyMetadata(false)
            );
        HasTextProperty = HasTextPropertyKey.DependencyProperty;
    }
}

public class TextBoxHelper
{
    public static string GetTitle(DependencyObject obj)
    {
        return (string)obj.GetValue(TitleProperty);
    }

    public static void SetTitle(DependencyObject obj, string value)
    {
        obj.SetValue(TitleProperty, value);
    }

    // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.RegisterAttached("Title", typeof(string), typeof(TextBoxHelper), new PropertyMetadata(""));
}

TextBox HasText

相关推荐
xcLeigh16 小时前
WPF在特定领域的应用:打造一款专业的图像编辑工具
c#·wpf
吾与谁归in1 天前
WPF给ListBox中的每一项添加右键菜单功能
c#·wpf
千里码!1 天前
RocketMQ延迟消息深度解析:原理、实践与性能调优
wpf·rocketmq
de之梦-御风2 天前
【.NET】WinForms 和 WPF 在性能方面的对比
.net·wpf
Zzu_zzx2 天前
c# wpf 开发中安装使用SqlSugar操作MySql数据库具体操作步骤保姆级教程
mysql·c#·wpf
白白白白纸呀2 天前
WPF框架---MvvmLight介绍
开发语言·c#·wpf
battlestar3 天前
WPF 解决加载顺序,Combox 增加属性,并关联text
windows·wpf
xcLeigh3 天前
WPF高级 | WPF 3D 图形编程基础:创建立体的用户界面元素
ui·3d·c#·wpf
一个处女座的程序猿O(∩_∩)O4 天前
鸿蒙与DeepSeek深度整合:构建下一代智能操作系统生态
华为·ai·wpf·harmonyos
为风而战4 天前
WPF+WebView 基础
wpf