0

I am trying to work with INotifyPropertyChanged and serial port. For some reason I am getting always property change null and I don't understand why. Why does this happen and what can I do about it?

Xaml Code:

<Label Grid.Column="1" x:Name="lblStatus" 
  Content="{Binding Path = SerialPortOpenStatus, UpdateSourceTrigger = PropertyChanged, 
    Converter = {StaticResource CComPortStatusTextCovertor}}"></Label>        

C# code:

public partial class MainWindow : Window, INotifyPropertyChanged
{
     private SerialPort sp;

    private bool _SerialPortOpenStatus;

    public bool SerialPortOpenStatus
    {
        get { return _SerialPortOpenStatus; }
        set { 
            _SerialPortOpenStatus = value;
            OnPropertyChanged("SerialPortOpenStatus");
        }
    }


    public MainWindow()
    {
        InitializeComponent();

        sp = new SerialPort();
        this.DataContext = this;
        lblStatus.DataContext = sp;            
        System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(CheckSerialPort));
        t.IsBackground = true;
        t.Start();



    }

    private void btnReset_Click(object sender, RoutedEventArgs e)
    {

    }


    private void CheckSerialPort()
    {
        while (true)
        {
            if (sp != null)
            {

                       _SerialPortOpenStatus = sp.IsOpen;

            }

            System.Threading.Thread.Sleep(500);
        }

    }




    public event PropertyChangedEventHandler PropertyChanged;



    // This method is called by the Set accessor of each property.
    // The CallerMemberName attribute that is applied to the optional propertyName
    // parameter causes the property name of the caller to be substituted as an argument.
    private void OnPropertyChanged( string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
 }
Barak Rosenfeld
  • 394
  • 5
  • 18
  • As a note `UpdateSourceTrigger=PropertyChanged` has nothing to with the PropertyChanged event of the INotifyPropertyChanged interface. Setting it here is pointless. – Clemens Mar 06 '18 at 08:26
  • 1
    That said, when you set `lblStatus.DataContext = sp;` the Binding expects the SerialPortOpenStatus property in `sp`. It ignores the SerialPortOpenStatus in your MainWindow class. JUst set `this.DataContext = this;` instead. Besides that, `sp` was still `null` when you assigned it to the DataContext. – Clemens Mar 06 '18 at 08:29
  • when you implement interface, try protected virtual void OnPropertyChanged(string propName) – Gaurang Dave Mar 06 '18 at 08:30
  • 1
    And ignore the previous comment. It makes no sense in this context. – Clemens Mar 06 '18 at 08:31
  • Is this literal: `this.DataContext = "Bla Bla";` ? Because it should go. SerialPortOpenStatus is not a property on "Bla Bla" – H H Mar 06 '18 at 08:36
  • Hi thanks to all answers I try all still properychange is null – Barak Rosenfeld Mar 06 '18 at 08:57
  • Please edit your question with what you have right now. And note that it doesn't matter whether you have `private void OnPropertyChanged` or `protected virtual void OnPropertyChanged`. It makes no difference. – Clemens Mar 06 '18 at 08:57
  • edit question per you answer property change still null – Barak Rosenfeld Mar 06 '18 at 09:03
  • There is still `lblStatus.DataContext = sp;` ?! – Clemens Mar 06 '18 at 09:26
  • 1
    so do I need to just remove it? – Barak Rosenfeld Mar 06 '18 at 09:27
  • Yes of course! As already explained above, it breaks your Binding. – Clemens Mar 06 '18 at 09:30
  • yes thanks that works well can you please explain again why lblStatus.DataContext = sp is not good? I did not understand it from you previous comment – Barak Rosenfeld Mar 06 '18 at 09:31
  • https://learn.microsoft.com/en-us/dotnet/framework/wpf/data/data-binding-overview – Clemens Mar 06 '18 at 09:35

0 Answers0