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;}
ViewModel:
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);
}
}
感谢您的帮助:)
答案 0 :(得分:0)
INotifyPropertyChanged接口用于通知客户端(通常是绑定客户端)属性值已更改。 -INotifyPropertyChanged上的Microsoft文档
您应该在模型BaseDParent上实现INotifyPropertyChanged,因为这是值在更改的地方,而不是在ViewModel中。
然后您可以从ViewModel中删除ConsoleText并直接绑定到BaseDParent.GroupContent以获得输出。
就目前而言,您在DConsoleViewModel上的DElement属性正在引发与对象引用本身有关的更改。例如从null到BaseDParent的构造版本。
有关此主题的Microsoft文档:https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.inotifypropertychanged?view=netframework-4.7.2