简体   繁体   English

C# WPF INotifyPropertyChanged 未更新表单

[英]C# WPF INotifyPropertyChanged not updating form

I am trying to implement a form with some calculations based on user inputs.我正在尝试使用基于用户输入的一些计算来实现一个表单。 Using the MVVM pattern, in particular INotifyPropertyChanged.使用 MVVM 模式,特别是 INotifyPropertyChanged。

All works well when the user enters a value in a textbox, the calculation routine fires, and the form updates with the result.当用户在文本框中输入一个值、计算例程触发并且表单随结果更新时,一切都运行良好。

However when the input is changed from codebehind, the Inotify routines fire, the calculation completes, but the bound controls don't update.但是,当从代码隐藏更改输入时,Inotify 例程会触发,计算完成,但绑定控件不会更新。

I have two problems:我有两个问题:

  1. Using pages within frames, I want to trigger a refresh when the page is changed使用框架内的页面,我想在页面更改时触发刷新
  2. Import previously saved data that is in an Xml file.导入以前保存在 Xml 文件中的数据。 Again, the routine fires, but no update to the form's bound controls.再次,例程触发,但没有更新窗体的绑定控件。

I have attached a condensed version of the code, but this isn't really the problem I don't think.我附上了代码的精简版本,但这并不是我认为的真正问题。 Note I am using a singleton class.注意我使用的是 singleton class。

Thanks谢谢

=============== ================

//INotify code
using s = Calc.Models.GlobalStrings;

namespace Calc.ViewModels.INotify
{
    public class UcIOChanged : INotifyPropertyChanged
    {
        private static UcIOChanged instance;
        public UcIOChanged() { }

        //Make the class is a singleton
        public static UcIOChanged Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new UcIOChanged();
                }
                return instance;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public string Pressure 
        { 
            get 
            { 
                return s.Pressure; 
            } 
            set 
            { 
                s.Pressure = value; OnPropertyChanged(); 
            } 
        }

        public void OnPropertyChanged()
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Pressure)));

        }
    }
}

Note I am using a singleton class.注意我使用的是 singleton class。

There are no errors in this code.这段代码没有错误。 But you may be using it incorrectly.但是您可能使用不正确。 To avoid accidental usage errors, I advise you to change the implementation:为避免意外使用错误,我建议您更改实现:

public class UcIOChanged
{
    // Hide the constructor to avoid
    // accidentally creating other instances.
    private UcIOChanged() {}

    //Make the class is a singleton
    public static UcIOChanged Instance { get; } = new UcIOChanged();

    public string Pressure
    {
        get
        {
            return s.Pressure;
        }
        set
        {
            if (!Equals(s.Pressure, value))
            {
                s.Pressure = value;
                PressureChanged?.Invoke(this, EventArgs.Empty);
            }

        }
    }

    // The event of the same name.
    // It's easier for one or two properties
    // than the INotifyPropertyChanged implementation.
    public event EventHandler PressureChanged;
}

Using:使用:

    <TextBox Text="{Binding Pressure, Source={x:Static local:UcIOChanged.Instance}}"/>

PS Correct operation may be affected if you change s.Pressure somewhere in your code other than through an instance of UcIOChanged.Instance. PS如果您在代码中的某个位置(而不是通过 UcIOChanged.Instance 的实例)更改s.Pressure ,则正确操作可能会受到影响。
Therefore, you must ensure that this variable (property or field?) is only accessed with notification of its change.因此,您必须确保只有在通知其更改时才能访问此变量(属性或字段?)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM