現時点では、これを解決する方法や修正方法のアイデアを見つける方法がわからない問題があります。子オブジェクトを含むビューモデルがあります。コマンドを発行すると、ChildObject を new で再初期化し、新しいデータを追加します。その後、ビューが更新され、新しいデータが表示されます。場合によっては (再現できない場合)、1 つのプロパティのみが更新されなくなります。オブジェクト全体の親でこれを行うか、String.Empty でこれを行うかに違いはありません。1 つのプロパティは更新されず、古い値のままになります (get メソッドは呼び出されません)。この動作を取得できたら、このコマンドを再度発行しても更新できません。
誰かが私を助けて、そのような行動がどこで発生する可能性があるかを知ることができますか?
ありがとう、
問題を可視化するためのサンプルコードを編集します。この場合は機能しているように見えますが、私のプロジェクトのコードはまったく同じ XAMLに見えます。
<Window x:Class="WpfApplication14.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<Button Click="Button_Click" Content="Text"/>
<Label Content="{Binding Path=Cls1.A}"/>
<Label Content="{Binding Path=Cls1.B}"/>
</StackPanel>
</Window>
ビューモデル:
using System.ComponentModel;
namespace WpfApplication14
{
class VM : INotifyPropertyChanged
{
public VM()
{
}
private int b;
public void doWork()
{
var cls2 = new Class2 { A = "XXX", B = "YYY", C = "ZZZ" };
if (b == 3)
{
cls2 = new Class2 { A = "AAA", B = "BBB", C = "CCC" };
}
if (b == 4)
{
cls2 = new Class2 { A = null, B = null, C = null };
}
b++;
if(b == 5)
{
b = 0;
}
Cls1 = new Class1(cls2);
OnPropertyChange("Cls1");
}
public Class1 Cls1 { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChange(string property)
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
}
クラス 2:
namespace WpfApplication14
{
class Class2
{
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
}
}
クラス1:
using System.ComponentModel;
namespace WpfApplication14
{
internal class Class1 : INotifyPropertyChanged
{
public Class1(Class2 x)
{
setCls2(x);
}
public Class2 cls2 { get; set; }
public string A
{
get
{
return cls2.A;
}
}
//After clicking e.g. this property stayes on the old value.
public string B
{
get
{
return cls2.B;
}
}
public string C
{
get
{
return cls2.C;
}
}
private void setCls2(Class2 x)
{
cls2 = x;
OnPropertyChange("");
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChange(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
}