所以我得到了这个DataGrid和以下类,但是当我编辑DataGrid中的一些值时,我不会进入DataNodeModel的PropertyChanged事件。
这是DataGrid:
<DataGrid
Grid.Row="0"
ItemsSource="{Binding ElementName=root, Path=Model.DataNodeModel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True}"
CanUserAddRows="True"
CanUserDeleteRows="True"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
CanUserSortColumns="True"
CanUserResizeColumns="True"
SelectedItem="{Binding ElementName=root, Path=Model.SelectedDataNodeModel, UpdateSourceTrigger=PropertyChanged}"
IsReadOnly="False"
IsEnabled="True"
>
<DataGrid.Columns>
<DataGridTextColumn
Header="Title"
Binding="{Binding Path=Title}"
Width="70"
/>
<DataGridTextColumn
Header="DefaultValue"
Binding="{Binding Path=DefaultValue}"
Width="70"
/>
<DataGridTextColumn
Header="Type"
Binding="{Binding Path=Type}"
Width="70"
/>
<DataGridTextColumn
Header="Id"
Binding="{Binding Path=Id, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True}"
Width="70"
/>
</DataGrid.Columns>
</DataGrid>
这也是我的DataNodeModel的类,它是我的DataGrid的ItemsSource:
public class DataNodeField : INotifyPropertyChanged
{
private string mDefaultValue, mId, mTitle, mType;
public string Id
{
get { return mId; }
set
{
mId = value;
OnPropertyChanged();
}
}
public string Title
{
get { return mTitle; }
set
{
mTitle = value;
OnPropertyChanged();
}
}
public string Type
{
get { return mType; }
set
{
mType = value;
OnPropertyChanged();
}
}
public string DefaultValue
{
get { return mDefaultValue; }
set
{
mDefaultValue = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
这也是我的模型的样子(删除了大部分内容,因为它没有添加到示例中:
internal partial class DataNodeViewModel {
...
private ObservableCollection<DataNodeField> mDataNodeModel;
private DataNodeField mSelectedDataNodeModel;
public DataNodeField SelectedDataNodeModel
{
get { return mSelectedDataNodeModel; }
set
{
mSelectedDataNodeModel = value;
OnPropertyChanged("SelectedDataNodeModel");
}
}
public ObservableCollection<DataNodeField> DataNodeModel
{
get { return mDataNodeModel; }
set
{
if (SelectedDataNodeModel != null)
{
//Do Stuff
}
mDataNodeModel = value;
OnPropertyChanged("DataNodeModel");
}
}
所以我真的无法弄清楚当我更改DataGrid中的值时,为什么我的DataNodeModel的PropertyChanged不会被执行。已经找了几天而且不知道为什么会这样。有人可以帮忙吗? (=