tags:

views:

67

answers:

2

This is probably a basic question, since I'm new to WPF..

I have a UserControl that contains a TextBox and a Button (code is simplified for this question) :

<UserControl x:Name="this">
    <TextBox Text="{Binding ElementName=this, Path=MyProperty.Value}"/>
    <Button x:Name="MyButton" Click="Button_Click"/>
</UserControl>

In the code behind I have registered "MyProperty" as DependencyProperty:

public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(MyProperty), typeof(MyPropertyNumeric), new UIPropertyMetadata(null));

A "MyProperty" is a class defined by me, that implements INotifyPropertyChanged. "MyProperty.Value" is of type object.

When the button is clicked, I change MyProperty.Value in the code-behind. I want to have the TextBox to automatically show the new value. I would expect that the above would work, since I've implemented INotifyPropertyChanged - but it doesn't.. Anyone knows how to do this?

+1  A: 

Are you calling the OnPropertyChanged event with the name of your property when it is updated?

Eg,

public class MyProperty : INotifyPropertyChanged {
  private string _value;

  public string Value { get { return _value; } set { _value = value; OnPropertyChanged("Value"); } }

  public event PropertyChangedEventHandler PropertyChanged;

  protected void OnPropertyChanged(string name) {
    PropertyChangedEventHandler handler = PropertyChanged;
    if(handler != null) {
      handler(this, new PropertyChangedEventArgs(name));
    }
  }
}

It is important to make sure the PropertyChanged event is fired with the name of the property you want to update.

Steven
Thanks! I assigned the name of the property (which has no meaning for WPF) instead of "Value"
Robbert Dam
A: 

try add to the Binding Mode="OneWay", i dont sure whats the defualt

Chen Kinnrot
Doesn't work, besides I want to make my binding work in both directions..
Robbert Dam