tags:

views:

24

answers:

1

Hi, I have simple class with one property, this class implements interface INotifyPropertyChange.

    public class SomeClass : INotifyPropertyChanged
    {
        private string _iD;

        public event PropertyChangedEventHandler PropertyChanged;

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

        public string ID
        {
            get { return _iD; }
            set
            {
                if (String.IsNullOrEmpty(value))
                    throw new ArgumentNullException("ID can not be null or empty");

                if (this.ID != value)
                {
                    _iD = value;
                    NotifyPropertyChanged(ID);
                }
            }
        }
    }

I try make o OneWay binding to label. I set label dataContext in codebehind.

    private SomeClass _myObject;

    public MainWindow()
    {
        InitializeComponent();
        _myObject = new SomeClass() { ID = "SomeID" };
        lb.DataContext = _myObject;
    }

In XAML I binding property ID from to Content of label.

    <Label Name="lb" Content="{Binding Path = ID, Mode=OneWay}" Grid.Row="0"></Label>
    <TextBox Name="tb" Grid.Row="1"></TextBox>
    <Button Name="btn" Content="Change" Height="20" Width="100" Grid.Row="2" Click="btn_Click"></Button>

Then I change value of property ID in button click event, but content of label isnt change.

    private void btn_Click(object sender, RoutedEventArgs e)
    {
        _myObject.ID = tb.Text;
        Title = _myObject.ID;
    }

Where can be problem?

+1  A: 

NotifyPropertyChanged should take the name of the changed property and not the value of the property. So change NotifyPropertyChanged(ID) to NotifyPropertyChanged("ID").

Jakob Christensen
Thank you for your advance
Tom