DependencyProperty.Register:wpf 向别的xaml传递参数

一.使用背景:在A.xaml中嵌入B.xaml,并且向B.xaml传递参数。

函数介绍:

csharp 复制代码
public static DependencyProperty Register(
	string name, 
	Type propertyType, 
	Type ownerType
);

name(string): 依赖属性的名称。在XAML中,使用这个名称来设置或绑定属性的值。
propertyType(Type): 依赖属性的数据类型。
ownerType(Type): 这是包含依赖属性的类的类型。通常是包含依赖属性注册代码的类的类型。


示例代码:在MainWindow.xaml中要嵌入UserControl1.xaml,并且向UserControl1.xaml传入参数。

MainWindow.xaml: (MainWindow.xaml.cs中没有添加别的内容。)

嵌入了一个UserControl1 ,给UserControl1 传递了一个字符串

xml 复制代码
<Window x:Class="Test1.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:Test1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <local:UserControl1 Message="Hello UserControl1, this Message from MainWindow.xaml" />
    </Grid>
</Window>

UserControl1.xaml:

xml 复制代码
<UserControl x:Class="Test1.UserControl1"
             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"
             mc:Ignorable="d"
             d:DesignHeight="300"
             d:DesignWidth="300">
    <Grid>
        <TextBlock Text="{Binding Message}" />
    </Grid>
</UserControl>

UserControl1.xaml.cs:

csharp 复制代码
namespace Test1
{
    /// <summary>
    /// UserControl1.xaml 的交互逻辑
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register("Message", typeof(string), typeof(UserControl1));
        public string Message
        {
            get { return (string)GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }
        public UserControl1()
        {
            InitializeComponent();
            DataContext = this;
        }
    }
}

运行结果:

二.使用背景:在A.xaml中嵌入B.xaml,并且向B.xaml传递参数,并且设置属性更改回调方法。

csharp 复制代码
public static DependencyProperty Register(
    string name,
    Type propertyType,
    Type ownerType,
    PropertyMetadata typeMetadata
);

name(string): 依赖属性的名称。在XAML中,使用这个名称来设置或绑定属性的值。
propertyType(Type): 依赖属性的数据类型。
ownerType(Type): 这是包含依赖属性的类的类型。通常是包含依赖属性注册代码的类的类型。
typeMetadata(PropertyMetadata): 这是一个 PropertyMetadata 对象,它包含关于依赖属性的元数据,例如默认值、属性更改回调等信息。


示例代码:在MainWindow.xaml中要嵌入UserControl1.xaml,并且向UserControl1.xaml传入参数,当参数改变时,触发回调函数。

MainWindow.xaml和UserControl1.xaml同上不变。
UserControl1.xaml.cs:

"Default Message" 是该依赖属性的默认值。当创建一个新的 UserControl1 实例时,如果没有为 Message 属性提供值,它将默认为 "Default Message"。(<local:UserControl1 />"运行结果为Default Message

OnMessagePropertyChanged 是一个委托,指定在 Message 属性的值更改时调用的回调方法。在这里,如果你的属性更改回调方法在 OnMessagePropertyChanged 中执行了一些逻辑,那么在属性更改时,这个逻辑就会被触发。

csharp 复制代码
namespace Test1
{
    /// <summary>
    /// UserControl1.xaml 的交互逻辑
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register("Message", typeof(string), typeof(UserControl1), 
                new PropertyMetadata("Default Message", OnMessagePropertyChanged));
        public string Message
        {
            get { return (string)GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }
        public UserControl1()
        {
            InitializeComponent();
            DataContext = this;
        }

        private static void OnMessagePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            // 处理属性更改的逻辑
            if (d is UserControl1 userControl)
            {
                string newMessage = (string)e.NewValue;
                // 处理属性更改的逻辑,这里简单地输出新值
                Debug.WriteLine($"MessageProperty changed to: {newMessage}");
            }
        }
    }
}

运行结果:

查看输出窗口:

相关推荐
△曉風殘月〆9 小时前
如何在WPF中捕获窗口外的事件
wpf
爱吃烤鸡翅的酸菜鱼2 天前
Java 事件发布-订阅机制全解析:从原生实现到主流中间件
java·中间件·wpf·事件·发布订阅
武藤一雄2 天前
WPF中ViewModel之间的5种通讯方式
开发语言·前端·microsoft·c#·wpf
CSharp精选营3 天前
都是微软亲儿子,WPF凭啥干不掉WinForm?这3个场景说明白了
c#·wpf·跨平台·winform
baivfhpwxf20233 天前
wpf TextBlock 控件如何根据内容换行?
wpf
亘元有量-流量变现3 天前
鸿蒙、安卓、苹果音频设备技术深度解析与开发实践
android·wpf·harmonyos·亘元有量·积分墙
软泡芙3 天前
【Bug】ReactiveUI WPF绑定中依赖属性不更新的问题分析与解决方案
java·bug·wpf
浪扼飞舟3 天前
WPF输入验证(ValidationRule)
java·javascript·wpf
IOFsmLtzR4 天前
Flink Agents 源码解读 --- (5) --- ActionExecutionOperator
microsoft·flink·wpf
廋到被风吹走6 天前
【AI】Codex 复杂任务拆解:从“一气呵成“到“步步为营“
人工智能·wpf