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}");
            }
        }
    }
}

运行结果:

查看输出窗口:

相关推荐
脚踏实地皮皮晨16 小时前
003006001_WrapPanel控件
开发语言·c#·.net·wpf·visual studio
脚踏实地皮皮晨17 小时前
003005002_WPF StackPanel 基类官方类定义逐行深度解析
开发语言·windows·算法·c#·wpf·visual studio
脚踏实地皮皮晨1 天前
003004002_UniformGrid控件的使用方法
开发语言·c#·.net·wpf·visual studio
脚踏实地皮皮晨2 天前
003003002_WPF Grid 基类官方类定义逐行深度解析
开发语言·windows·算法·c#·wpf·visual studio
脚踏实地皮皮晨3 天前
003002004_WPF Panel 基类 官方类定义
开发语言·windows·算法·c#·wpf·visual studio
湿滑路面3 天前
Rouyan:使用WPF/C#构建的基于LLM的快捷翻译小工具
开发语言·c#·wpf
软萌萌的13 天前
C#中的多级缓存架构设计与实现深度解析
缓存·c#·wpf
dalong104 天前
WPF:3D正八面体自动旋转
3d·wpf
仙宇觉尘4 天前
WPF Stylet可以如何实现导航功能?
java·开发语言·wpf
丙氨酸長鏈4 天前
WPF 引用 ASP.NET Core 的 AOT 版本
后端·asp.net·wpf