【问题标题】:WPF : Notify model property from another view modelWPF:从另一个视图模型通知模型属性
【发布时间】:2018-11-11 15:04:58
【问题描述】:

我是 WPF MVVM 的新手

我在这里有 3 个类(所有类都有相同的 BaseParent):

---- 问题是----> 每当我在 [DPropertyViewModel] 中设置 BaseParents GroupContent 时,它都不会更新 [DConsoleViewModel] 中的 ConsoleText。

型号:

 public class BaseDParent
{
    public string GroupType { get; set; } = "None";

    public string GroupName { get; set; } = "None";

    public string GroupContent
    {
        get => _groupcontent;
        set
        {
            _groupcontent = value;
            SetContent();
        }
    }
    private string _groupcontent;}

视图模型:

 public class DConsoleViewModel : DNotify
{
    public BaseDParent DElement
    {
        get => _delement;
        set
        {
            _delement = value;
            NotifyPropertyChanged();
        }
    }
    private BaseDParent _delement;

    public string ConsoleText
    {
        get => DElement.GroupContent;
        set
        {
            DElement.GroupContent = value;
            NotifyPropertyChanged();
        }
    }}

这是我更改 BaseParents GroupContent 的地方:

public class DPropertyViewModel : DNotify
{
    public BaseDParent DElement
    {
        get => _delement;
        set
        {
            _delement = value;
            NotifyPropertyChanged();
        }
    }
    private BaseDParent _delement;

    public string Level { get; set; } = "0"; 
    public string Property { get; set; } = "Property"; 
    public string Value { get; set; } = "-Null-";

    public void SetDElementPropertyValue()
    {
        string startline = " " + DElement.GroupType + " " + DElement.GroupName + DHelper.NewLine();
        string subpropline = DHelper.NewLine() + Level + " " + Property + " ";

        int start = DElement.GroupContent.IndexOf(startline);

        int propstart = DElement.GroupContent.IndexOf(subpropline, start - 3) + subpropline.Length;
        int propnext = DElement.GroupContent.IndexOf(DHelper.NewLine(), propstart);
        string propvalue = DElement.GroupContent.Substring(propstart, propnext - propstart);

        string toremove = subpropline + propvalue + DHelper.NewLine();
        int toaddindex = DElement.GroupContent.IndexOf(toremove);
        DElement.GroupContent = DElement.GroupContent.RemoveSub(toremove);

        string toadd = subpropline + Value + DHelper.NewLine();
        DElement.GroupContent = DElement.GroupContent.Insert(toaddindex, toadd);
    }
}

感谢您的帮助:)

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案1】:

    INotifyPropertyChanged 接口用于通知客户端(通常是绑定客户端)属性值已更改。 - 关于 INotifyPropertyChanged 的​​ Microsoft Docs

    您的模型 BaseDParent 是您应该实施 INotifyPropertyChanged 的​​地方,因为这是值发生变化的地方,而不是在 ViewModel 中。

    然后,您可以从 ViewModel 中删除 ConsoleText 并直接绑定到 BaseDParent.GroupContent 以进行输出。

    就目前而言,您在 DConsoleViewModel 上的 DElement 属性正在引发与对象引用本身相关的更改。例如从 null 到 BaseDParent 的构造版本。

    有关该主题的 Microsoft Docs:https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.inotifypropertychanged?view=netframework-4.7.2

    【讨论】:

      猜你喜欢
      • 2014-10-06
      • 2012-12-30
      • 1970-01-01
      • 1970-01-01
      • 2011-08-09
      • 1970-01-01
      • 2011-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多