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


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

    函数介绍:

    public static DependencyProperty Register(
    	string name, 
    	Type propertyType, 
    	Type ownerType
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    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;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    运行结果:
    在这里插入图片描述

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

    public static DependencyProperty Register(
        string name,
        Type propertyType,
        Type ownerType,
        PropertyMetadata typeMetadata
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    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}");
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    运行结果:
    在这里插入图片描述
    查看输出窗口:
    在这里插入图片描述

  • 相关阅读:
    Win10系统打开组策略编辑器的两种方法
    ref 操作 React 定时器
    【C基础篇】算法、数据类型、运算符与表达式、常见输入出函数
    <二.1> android 直接使用hal库播放pcm demo
    springcloud3 指定nacos的服务名称和配置文件的group,名称空间
    尚医通 (二十六) --------- 科室接口开发
    js禁用浏览器 pdf 打印、下载功能(pdf.js 禁用打印下载、功能)
    欣赏GitHub爆火的150k的1000页核心大数据算法文档
    【概率论基础进阶】随机变量及其分布-随机变量函数的分布
    STM32G0开发笔记-Platformio+libopencm3-FreeRTOS和FreeModbus库使用
  • 原文地址:https://blog.csdn.net/modifier_/article/details/134558437