0

So I've been sifting through similar questions on SO for a few days now. I just want to know why this problem occurs. I've got a Class with properties and a SeriesCollection used for Live Charts, which are binded to the UI. Since the properties need to be able to be Serialized, the SeriesCollection can not be part of the specific modelview (but need to binded to UI for plotting a chart). like so:

public class DLVOModel
{
    public SeriesCollection table { get; set; }
    public DLVOConfiguration DLVOConfiguration { get; set; }
}
public partial class DLVOModelizer : Window
{
    public DLVOModel model { get; set; }
    public DLVOModelizer()
    {
        InitializeComponent();
        model = CreateModel();
        DataContext = model; //Databinding
    }
    private DLVOModel CreateModel() => new DLVOModel()
    {
        DLVOConfiguration = new DLVOConfiguration(),
        table = new SeriesCollection(),
    };
public class DLVOConfiguration
{
    public double HMax { get; set; }
    public int Resolution { get; set; }
    //... about 25 properties
}

`

XAML:

<window>
<lvc:CartesianChart Series="{Binding Path=table}"/>
<GroupBox DataContext="{Binding DLVOConfiguration}">
    <Grid>
        <TextBox Text="{Binding Path=HMax, Mode=TwoWay, 
UpdateSourceTrigger=PropertyChanged}"/>
        <TextBox Text="{Binding Path=Resolution, Mode=TwoWay, 
UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</GroupBox>

So this all works well untill I try to deserialize an xml file. The model gets updated properly but the UI falls behind. The textboxxes updates to the models value when I try to change their text. This is odd because:

  1. The Databinding works fine, so UI should update immediately.
  2. When entering a new value in the UI the model property should change, not the UI.

(Also tried a version without UpdateSourceTrigger).

Before I binded directly to the DLVOConfiguration and everything worked fine.

I know there is a way in which your modelview inherits from INotifyPropertyChanged, but for some reason I run into the same problem. EDIT: I added code for the case in which I used INotifyPropertyChanged from this question: WPF DataBinding not updating?

    public class DLVOConfiguration : INotifyPropertyChanged
{
    private double _HMax;
    public double HMax
    {
        get { return _HMax; }
        set
        {
            _HMax = value;
            NotifyPropertyChanged("HMax");
        }
    }
    private int _Resolution;
    public int Resolution
    {
        get { return _Resolution; }
        set
        {
            _Resolution = value;
            NotifyPropertyChanged("Resolution");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
  • 1
    ViemModel with INotifyPropertyChanged is the right way to go. Can you show what you tried in this direction and what did not work? – Klaus Gütter Jan 20 '19 at 13:20

1 Answers1

0

I guess you're replacing the instance being bound to somewhere. That breaks the data binding. As long as you're merely updating the properties with new values, it should work just fine.

Droppy
  • 51
  • 1
  • 3