フロントエンドのデータグリッドをビューモデルの elementDetail プロパティにバインドするこのコードがあります。elementDetail は、独自の INotifyPropertyChanged クラスのインスタンスとして定義されています。しかし、PropertyChanged ハンドルが常に null になるのはなぜでしょうか? データテーブルをデータグリッドにバインドするのは正しい方法であり、データグリッドはデータテーブルへの変更に応答する必要がありますか?
WPF プログラムをデバッグする最良の方法は何ですか? バインドのために diag:PresentationTraceSources.TraceLevel=High をオンにしましたが、提供される情報はあまり多くありません。
XAML
<Grid Name="myGrid" Background="#FFF3FF76">
<DataGrid Name="detailGrid"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="10,57,0,0"
Width="Auto"
Height="56"
Background="#FFFFDF9F"
ItemsSource="{Binding elementDetail, Mode=OneWay,UpdateSourceTrigger=PropertyChanged, diag:PresentationTraceSources.TraceLevel=High}" />
</Grid>
コード ビハインド .xaml
this.myGrid.DataContext = myViewModelInstance;
私のViewModelで
public ObservableDataView elementDetail
{
get;
set;
}
データテーブルを変更する
elementDetail.data = dt;
ここで INotifyPropertyChanged クラスを定義しました
public class ObservableDataView : INotifyPropertyChanged
{
private string _propertyName;
public ObservableDataView(string propertyName)
{
_propertyName = propertyName;
}
private DataTable _data;
public DataTable data
{
get{ return _data; }
set
{
_data = value;
onChanged(_propertyName);
}
}
public void onChanged(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
PropertyChanged(this, e);
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
編集:私の質問への回答
onChange("data") と言うべきです
また、以下のようにバインディングに .data を追加する必要があります
<DataGrid Name="detailGrid" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,57,0,0" Width="Auto" Height="56" Background="#FFFFDF9F" ItemsSource="{Binding elementDetail.data, Mode=OneWay,UpdateSourceTrigger=PropertyChanged, diag:PresentationTraceSources.TraceLevel=High}" />
現在、正常に動作しています。バインドをデバッグする方法はありますか、またはバインドするための一般的なルールはありますか? 現在、WPF で直面している主な問題は、間違ったバインディングであることがわかりました。