WPF用户控件绑定到对象don';不起作用

本文关键字:不起作用 don 对象 用户 控件 绑定 WPF | 更新日期: 2023-09-27 18:29:57

我在将用户控件绑定到主窗口中的对象时遇到问题。我不知道怎么了。

我创建了自定义控件MyUserControl,它有可编辑的文本框

MyUsrControl.xaml

<UserControl x:Class="UserControlToObject.MyUsrControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"              
             mc:Ignorable="d" 
             d:DesignHeight="70" d:DesignWidth="200">    
    <StackPanel Orientation="Horizontal">
        <Label Grid.Row="0" Grid.Column="0" Margin="5">Name</Label>            
        <TextBox Grid.Row="0" Grid.Column="1" Margin="5" Name="tbxName"></TextBox>
    </StackPanel>
</UserControl>

接下来,我定义了DP,以允许在countrol 之外修改此文本框

MyUsrControl.xaml.cs

namespace UserControlToObject
{
    public partial class MyUsrControl : UserControl
    {
        public static readonly DependencyProperty EmpNameProperty = DependencyProperty.Register("EmpNameProperty", typeof(string), typeof(MyUsrControl),           
                               new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, EmpNamePropertyChanged));
        public string EmpName
        {
            get
            {
                return (string)GetValue(EmpNameProperty);
            }
            set
            {
                SetValue(EmpNameProperty, value);
            }
        }
        public MyUsrControl()
        {
            InitializeComponent();            
        }
        static void EmpNamePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            MyUsrControl x = (MyUsrControl)sender;
            x.tbxName.Text = (string)e.NewValue;
        }
    }
}

接下来,我定义了Employee类——该类对象的属性将显示在用户控件上

namespace UserControlToObject
{
    /// <summary>
    /// Employee class
    /// </summary>
    class Employee : INotifyPropertyChanged
    {
        string m_name, m_surname;
        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// Employee name property
        /// </summary>
        public string Name
        {
            get { return m_name; }
            set
            {
                m_name = value;
                OnPropertyChanged("Name");
            }
        }
        /// <summary>
        /// Employee surname property
        /// </summary>
        public string Surname
        {
            get { return m_surname; }
            set
            {
                m_surname = value;
                OnPropertyChanged("Surname");
            }
        }
        public Employee()
        {
            m_name = "unknown name";
            m_surname = "unknown surname";
        }
        public Employee(string name, string surname)
        {
            m_name = name;
            m_surname = surname;
        }    
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }

最后是MainWindow.xaml

<Window x:Name="myApp" x:Class="UserControlToObject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:UserControlToObject"        
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <local:MyUsrControl x:Name="ucEmp" EmpName="{Binding Name}"></local:MyUsrControl>
        <Label Content="{Binding ElementName=ucEmp, Path=EmpName}"></Label>
    </StackPanel>
</Window

>

主窗口.xaml.cs

namespace UserControlToObject
{
    public partial class MainWindow : Window
    {
        Employee anEmployee;
        public MainWindow()
        {
            InitializeComponent();
            anEmployee = new Employee("John", "Wayne");
            this.DataContext = anEmployee;         
        }
    }

}

这一行不起作用(错误说我只能在DependencyObject的DependencyProperty…上设置绑定):

<local:MyUsrControl x:Name="ucEmp" EmpName="{Binding Name}"></local:MyUsrControl>

下面的设置有效,所以我认为这是我的员工类的问题(缺少什么?)

<local:MyUsrControl x:Name="ucEmp" EmpName="John"></local:MyUsrControl> --> set EmpName ok
<Label Content="{Binding ElementName=ucEmp, Path=EmpName}"></Label> --> get EmpName ok

我不知道哪里出了问题,所以非常乐意寻求帮助

我做了同样的操作,但绑定了TextBox而不是用户控件,没有问题。TextBox.Text是与Employee.EmpName 相同的依赖属性

WPF用户控件绑定到对象don';不起作用

注册依赖项属性时,需要使用要在XAML中使用的名称。在您的情况下,这是EmpName,而不是EmpNameProperty:

public static readonly DependencyProperty EmpNameProperty = DependencyProperty.
    Register(nameof(EmpName), typeof(string), typeof(MyUsrControl), new 
    FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.
    BindsTwoWayByDefault, EmpNamePropertyChanged));

您的财产注册中的"EmpNameProperty"应为"EmpName"w.

感谢