WPF 依赖项属性

WPF中的依赖项属性

对比与C#的属性。依赖属性是为WPF创建的。WPF很多情况下都在使用依赖属性。

1 依赖属性加入了属性变化通知,限制,验证功能。

2 节约内存

3 通过多种方式设置依赖属性的值

把属性换成依赖属性

1 依赖属性继承自 DependencyObject

2 使用public static 声明一个变量

public static readonly DependencyProperty MyPropertyProperty

3 再进行 Register 注册

复制代码
public class Person: DependencyObject
{
    static Person()
    {

    }
    public int MyProperty
    {
        get { return (int)GetValue(MyPropertyProperty); }
        set { SetValue(MyPropertyProperty, value); }
    }
    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MyPropertyProperty =
        DependencyProperty.Register("MyProperty", typeof(int), typeof(Person), new PropertyMetadata(0));
}
复制代码

依赖属性的继承

复制代码
<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        FontSize="30"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <TextBlock Text="123"/>
        <TextBlock Text="123" FontSize="15"/>
    </StackPanel>
</Window>
复制代码

window上的fontsize会影响到所有的子元素字体大小。

 

 通过AddOwer进行依赖属性继承

复制代码
public class CustomStackPanel:StackPanel
{
    static CustomStackPanel()
    {
        MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(string), typeof(CustomStackPanel), new FrameworkPropertyMetadata("23333", FrameworkPropertyMetadataOptions.Inherits));
    }

    public string MyProperty
    {
        get => (string)GetValue(MyPropertyProperty);
        set => SetValue(MyPropertyProperty, value);
    }

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MyPropertyProperty;
}

public class CustomButton : Button
{
    private static readonly DependencyProperty MyDependencyProperty;

    static CustomButton()
    {
        MyDependencyProperty = CustomStackPanel.MyPropertyProperty.AddOwner(typeof(CustomButton),
            new FrameworkPropertyMetadata("233333", FrameworkPropertyMetadataOptions.Inherits));
    }

    public string MyProperty
    {
        get => (string) GetValue(MyDependencyProperty);
        set => SetValue(MyDependencyProperty, value);
    }
}
复制代码

只读依赖属性

DependencyProperty.RegisterAttachedReadOnly

 

posted @   樱花落舞  阅读(359)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示