绑定到对象的WPF usercontrol不起作用

时间:2013-09-02 09:52:46

标签: c# wpf xaml user-controls

我在将我的usercontrol绑定到mainwindow中的对象时遇到问题。我不知道出了什么问题。

我创建了自定义控件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

&GT;

MainWindow.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

我不知道whot是错的,所以非常有帮助             

我做了相同但绑定TextBox而不是用户控件,没有问题。 TextBox.Text是与Employee.EmpName

相同的依赖属性

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

您的财产注册中的“EmpName Property”应为“”EmpName“”w。

由于