C# – Textbox + binding to Property with INotifyPropertyChanged from different assembly

bindingctextboxwpf

I have a problem with updating the value of property from code which is bind to textbox (the textbox is not displaying new value).

It was normally working without any problems but in this case source class is in separate assembly (Don't know if it is making any difference).

  1. If I type in textbox the value will be updated in the code
  2. When I change the property value directly in code textbox is not displaying new value.
  3. After changing the value in textbox one more time the previously set value (in code) is overwritten (so binding is still working).

Additionally I've checked if PropertyChanged event is fired and it is after every change.

Any guesses why it's not working? Below corresponding binding and source class.

TextBox Text="{Binding Path=Description, Mode=TwoWay}"

[DataContract]
public class Source : INotifyPropertyChanged
{
    private String _Description;

    [DataMember]
    public String Description
    {
        get { return _Description; }
        set
        {
            if (_Description == value)
                return;

            _Description = value;
            OnPropertyChanged("Description");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }
}

I've Fix the problem it was not connected to different assembly obviously. I have a bug in setting value to property.

Best Answer

It works fine by me. The only difference is DataContract tag removed and DataContext assigned. and My sample is following:

<Window x:Class="_4206499.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="360" Width="578">
<Grid>
    <TextBox Text="{Binding Path=Description, Mode=TwoWay}" VerticalAlignment="Center" Margin="12,12,286,278" />
    <Button Width="100" Margin="57,59,346,219" Click="Button_Click"></Button>
</Grid>

and code behind is

using System;
using System.Windows;
using ClassLibrary1;

namespace _4206499 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Source = new Source(); DataContext = Source; }

    public Source Source { get; set;}

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Source.Description = DateTime.Now.ToString();
    } 
}

}

and one from separate class library:


using System;
using System.ComponentModel;

namespace ClassLibrary1 { public class Source : INotifyPropertyChanged { private String _Description;

    public String Description
    {
        get { return _Description; }
        set
        {
            if (_Description == value)
                return;

            _Description = value;
            OnPropertyChanged("Description");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }
}

}

Related Topic