1

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

我创建了具有可编辑文本框的自定义控件 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 以允许在控件之外修改此文本框

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>

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

<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 相同的依赖属性

4

2 回答 2

4

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

public static readonly DependencyProperty EmpNameProperty = DependencyProperty.
    Register(nameof(EmpName), typeof(string), typeof(MyUsrControl), new 
    FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.
    BindsTwoWayByDefault, EmpNamePropertyChanged));
于 2013-09-02T09:56:52.157 回答
0

您的财产登记中的“EmpNameProperty”应该是“”EmpName“”w。

谢谢

于 2013-09-02T10:01:20.843 回答