开发者

How to bind a simple .net property to a label and have it auto update

I'm kinda new to data binding and obviously don't really understand it. I'm trying to bind a simple .net property to a label, and have that label updated whenever the property is changed. It's not working and i'm not sure where the problem lies.

Here is my XAML

 <Label Content="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}"></Label>

 <Button Name="bChangeProperty" Click="bChangeProperty_Click">Change Property</Button>

Here is my UserControl cs file

public partial class MyUserControl : UserControl, INotifyPropertyChanged
{

   private MyObjectClass _myObject;
   public MyObjectClass MyProperty
   {
       get { return _myObject;}
       set
       {
          if (_myObject != value)
          {
              _myObject = value;
              OnPropertyChanged("MyProperty"); 
          }
        }
   }


   public MyUserControl(MyObjectClass obj)
   {
      if (obj == null)
      {
          obj = new MyObjectClass();
          obj.Name = "Frank";
      }
      MyProperty= obj;
      base.DataContext = MyProperty;
   }

   //INotifyPropertyChanged stuff
   public event PropertyChangedEventHandler PropertyChanged;
   private void OnPropertyChanged(string propertyName)
   {
      //PropertyChanged is always null, don't really understand this part
      if (PropertyChanged != null)
         PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
   }


   private void bChangeProperty_Click(object sender, RoutedEventArgs e)
   {
       //Create a new object, and set it to the MyProperty Property - which I thought
       //would update the label, but it doesn't
       MyObjectClass newObject = new MyObjectClass();
       newObject.Name = "Bob";

       MyProperty= newObject;
   }

Here is the MyObjectClass

public class MyObjectClass
{
    public string Name {get; set;}
}

The inital bind works, it will set the Label to "Frank", but when I click the button and the object is changed, the label won't update. This is just an example of something "like" what i'm trying to do.

Do I have to set PropertyChanged to something somewhere? I kinda thought thats what the XAML was suppose to do, but seeing how it is al开发者_如何学Cways null, apparently it's not.


You need to implement INotifyPropertyChanged on MyObjectClass and fire PropertyChanged after MyObjectClass.Name was changed.

public class MyObjectClass : INotifyPropertyChanged
{
    private string name;

    public string Name 
    {
        get
        {
            return this.name;
        }
        set
        {
            this.name = value;
            OnPropertyChanged("Name"); 
        } 
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {      
       if (PropertyChanged != null)
          PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}


You are setting the DataContext in your constructor to point to the initial "Frank" object, but when you change the instance that MyProperty points to you do not update the DataContext accordingly.

Therefore, the direct solution is to add this:

private void bChangeProperty_Click(object sender, RoutedEventArgs e)
{
   //Create a new object, and set it to the MyProperty Property - which I thought
   //would update the label, but it doesn't
   MyObjectClass newObject = new MyObjectClass();
   newObject.Name = "Bob";

   MyProperty= newObject;
   this.DataContext = MyProperty; // ADD THIS LINE
}

However, the correct solution is to not set the DataContext at all and simply change the Binding:

<Label Content="{Binding Path=MyProperty.Name} ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜