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

运行结果:

查看输出窗口:

相关推荐
佛·追命1 小时前
.net wpf混淆
.net·wpf
weixin_447103583 小时前
Wpf布局之StackPanel!
wpf
小老鼠爱大米3 小时前
[C#] WPF - 资源URI
c#·wpf·uri
三千道应用题11 小时前
WPF学习笔记(13)列表框控件ListBox与数据模板
wpf
甄天10 天前
WPF中MVVM和MVVMLight模式
c#·wpf·visual studio
CoderIsArt12 天前
C# WPF常用调试工具汇总
开发语言·c#·wpf
凉、介12 天前
SylixOS 下的消息队列
linux·wpf·sylixos
Magnum Lehar12 天前
wpf3d游戏引擎ProjectLayoutView实现
游戏引擎·wpf
摆烂的少年12 天前
WPF中自定义DataGrid表格后,修改选中行的字体颜色或背景图
wpf
CoderIsArt12 天前
WPF调试三种工具介绍:Live Visual Tree、Live Property Explorer与Snoop
wpf