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

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

函数介绍:

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 传递了一个字符串

<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:

<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:

namespace Test1
{
    /// 
    /// UserControl1.xaml 的交互逻辑
    /// 
    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;
        }
    }
}

运行结果:
DependencyProperty.Register:wpf 向别的xaml传递参数_第1张图片

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

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”。("运行结果为Default Message
OnMessagePropertyChanged 是一个委托,指定在 Message 属性的值更改时调用的回调方法。在这里,如果你的属性更改回调方法在 OnMessagePropertyChanged 中执行了一些逻辑,那么在属性更改时,这个逻辑就会被触发。

namespace Test1
{
    /// 
    /// UserControl1.xaml 的交互逻辑
    /// 
    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}");
            }
        }
    }
}

运行结果:
DependencyProperty.Register:wpf 向别的xaml传递参数_第2张图片
查看输出窗口:
DependencyProperty.Register:wpf 向别的xaml传递参数_第3张图片

你可能感兴趣的:(c#,wpf)