0

I'm troubling to resolve how a textbox should automatically change its value when a object is updated. I have a Temperature class with Name and Value.

public class Temperature
{
    public string Name { get; set; }
    public double Value { get; set; }
}

In MainWindow.xaml.cs after InitializeComponent(), I create an ObservableCollection _lstTempObs = new ObservableCollection(); and then I add an initial temperature object.

In the main window I have a textbox

<TextBox x:Name="T1"  Text="{Binding Path=Value}" HorizontalAlignment="Left" Height="23" Margin="215,55,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>

And in MainWindow.xaml.cs I set the datacontext as T1.DataContext = _lstTempObs.LastOrDefault(); The temperature update comes from an API. When I get the mew Temperature object, I add it in the ObservableCollection, but the value isn't change in the GUI.

 public partial class MainWindow : Window
{
    ObservableCollection<Temperature> _lstTempObs = new ObservableCollection<Temperature>();
    public MainWindow()
    {
        InitializeComponent();
        _lstTempObs.Add(new Temperature { Name = "T1", Value = "0.321" });

        T1.DataContext = _lstTempObs.LastOrDefault();
    }
}
nuclectro
  • 31
  • 1
  • 5
  • 1
    What's the purpose of the ObservableCollection when you seemingly only want to show the last element? – Clemens Jun 18 '19 at 10:04

1 Answers1

0

you need to implement a notification-mechanism that informs the UI of changes to the data. In WPF this is usually done using INotifyPropertyChanged-interface. Check this answer for a basic reference on how to do that.

In a actual scenario I would implement the INPC-interface in a base class

public class INPCBase : INotifyPropertyChanged
{
    public bool SetField<U>(ref U field, U value, [CallerMemberName] string propertyName = null)
    {
        if (field == value))
        {
            return false;
        }

        field = value;
        OnPropertyChanged(propertyName);
        return true;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public virtual void OnPropertyChanged([CallerMemberName]string propertyName = "")
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }

} 

Usage:

public class Temperature : INPCBase
{
    private string name;
    public string Name { get; set{base.SetField(ref name, value); } }

    private double val;
    public double Value { get; set{base.SetField(ref val, value); } }
}
Sancho Panza
  • 670
  • 4
  • 11
  • I'm not sure why your answer was down-voted. Maybe your code is a bit complicated for a beginner who is unaware of inpc, You could lead with at least a link to the simple implementation on msdn with no equality comparer. ( FWIW I upvoted ) – Andy Jun 18 '19 at 11:10
  • @SanchoPanza Don't know why also. I upvoted your answer and comment since it was below 0. I also think maybe its a bit complicated, perhaps just keep the important stuff, like implementing INotifyPropertyChanged and what you need to implement that etc Cos some of the code doesn't seem to be part of the code in the question. – pm101 Jun 18 '19 at 11:12
  • thanks for the input (@Andy/@pm101) I tried to simplify the code and make it focus on OPs question. – Sancho Panza Jun 18 '19 at 11:32