Bartosz
Bartosz

Reputation: 4786

Databinding not working for user control - PropertyChanged always null

I have a problem with data binding. A test application that I have looks as follows:

There's a mainwindow:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Controls="clr-namespace:WpfApplication2"
        x:Name="main"
        Title="MainWindow" >
    <StackPanel >
        <Button Content="Button" Click="Button_Click"/>
        <Controls:UserControl1 />
    </StackPanel>
</Window>

And a user control:

    <UserControl x:Class="WpfApplication2.UserControl1"
                 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" 
                 x:Name="uc"
                 >
        <Grid >
            <TextBox Width="40" Text="{Binding ElementName=main, 
Path=Status, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        </Grid>
    </UserControl>

I want to click the button on the main window to have the value of text box in user control updated:

The code of MainWindow file:

namespace WpfApplication2
{

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        private string _status;
        public string Status
        {
            get { return _status; }
            set
            {
                if (value != _status)
                {
                    _status = value;
                    RaisePropertyChanged("Status");
                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (Status == "one")
                Status = "two";
            else
                Status = "one";
        }
    }
}

And the code of UserControl:

namespace WpfApplication2
{
        public partial class UserControl1 : UserControl, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, e: new PropertyChangedEventArgs(propertyName));
        }

        public UserControl1()
        {
            InitializeComponent();
        }
    }
}

I don't understand why doesn't it work, but the PropertyChanged is always null. The example is in the simplest form I can imagine...

Upvotes: 0

Views: 871

Answers (1)

Mike Eason
Mike Eason

Reputation: 9723

You are trying to access the parent window using the ElementName binding, as far as I am aware, that is not possible. You can however use a relative source binding to get the parent window:

<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=Status}" ... />

Follow up edit:

Your child user control should look like this:

<UserControl 
         ...
         x:Name="usr">
   <Grid>
      <TextBlock Text="{Binding Message, ElementName=usr}"/>
   </Grid>
</UserControl>

You will then need to create a dependency property called 'Message' (This is just an example, I'm not sure what you want to call this property).

public partial class YourUserControl: UserControl
{
    public string Message
    {
        get { return (string)GetValue(MessageProperty); }
        set { SetValue(MessageProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Message.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MessageProperty =
        DependencyProperty.Register("Message", typeof(string), typeof(YourUserControl), new PropertyMetadata(""));

    public UserControl1()
    {
        InitializeComponent();
    }
}

Then, when you declare this in your parent user control, simply set the binding of the Message property to whatever property you need to bind to in your parent user control:

<YourNamespace:YourUserControl Message="{Binding PropertyName, ElementName=elementName}" />

Upvotes: 3

Related Questions