czechoww
czechoww

Reputation: 43

WPF usercontrol binding to object don't work

I have a problem with binding my usercontrol to object in mainwindow. I don't know what is wrong.

I created custom control MyUserControl which has editable textbox

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>

Next I defined DP to allow modify this textbox outside 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;
        }
    }
}

Next, I defined Employee class - properties of object of this class will be displayed on user control

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));
            }
        }

    }

And finally 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

>

MainWindow.xaml.cs

namespace UserControlToObject
{
    public partial class MainWindow : Window
    {
        Employee anEmployee;

        public MainWindow()
        {
            InitializeComponent();
            anEmployee = new Employee("John", "Wayne");

            this.DataContext = anEmployee;         
        }

    }

}

This line don't work (error saying that I can set binding only on DependencyProperty... of DependencyObject):

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

Those settings below works, so I think that's problem with my Employee class (sth is missing ?)

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

I've no idea whot is wrong, so will be very greatfull for help

I did the same but binding TextBox instead of user control and was no problem. TextBox.Text is dependency property same as my Employee.EmpName

Upvotes: 1

Views: 2061

Answers (2)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174299

When registering the dependency property you need to use the name that you want to use in XAML. In your case, this is EmpName and not EmpNameProperty:

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

Upvotes: 4

Nitin Purohit
Nitin Purohit

Reputation: 18580

"EmpNameProperty" in your property registeration should be ""EmpName"" w.

Thanks

Upvotes: 0

Related Questions