我在WPF中有一个绑定到可观察集合的DataGrid。因为我需要监控哪些特定记录是“脏”的。我已将我的数据类指定为INotifiable,并设置了'IsDirty'属性作为每个字段的setter的一部分。
我的DataGrid有两个列,一个文本列和一个组合框列。当文本列发生更改时,会调用相应属性的setter - 但是当组合框更改时,不调用该属性的setter - 任何我出错的想法?
编辑:我指出数据最初正确加载,并且我能够更新UI中的组合框。此外,每当对网格进行任何更改时,它都会获得红色边框,就像绑定失败一样。但是,当我更改名称列并且工作正常时,它也会得到这个,所以不确定是否链接
编辑:我还尝试添加一个选择更改事件,以查看是否会触发它。然而,设置者固执地不会被调用,因此我的基础数据不会被更新
查看模型:
public class ContractConfigViewModel : INotifyPropertyChanged
{
public class ClientSurveyor: INotifyPropertyChanged
{
public ClientSurveyor()
{
IsDirty = false;
}
public bool IsDirty;
private ZoomLineManager LineManager;
public ZoomLineManager linemanager
{
get
{
return LineManager;
}
set
{
LineManager = value;
IsDirty = true;
OnPropertyChanged("linemanager");
}
}
private string Name;
public string name
{
get
{
return Name;
}
set
{
Name = value;
IsDirty = true;
OnPropertyChanged("name");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string PropertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
}
}
private ObservableCollection<ClientSurveyor> allClientReps;
public ObservableCollection<ClientSurveyor> allclientreps
{
get
{
return allClientReps;
}
set
{
allClientReps = value;
OnPropertyChanged("allclientreps");
}
}
private ObservableCollection<ZoomLineManager> allLineManagers;
public ObservableCollection<ZoomLineManager> alllinemanagers
{
get
{
return allLineManagers;
}
set
{
allLineManagers = value;
OnPropertyChanged("alllinemanagers");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string PropertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
}
}
WPF:
<DataGrid Margin="5" Grid.ColumnSpan="4" ItemsSource="{Binding Path=allclientreps}" SelectedItem="{Binding Path=selectedclientrep}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Representative Name" Width="*" Binding="{Binding Path=name, NotifyOnTargetUpdated=True}"/>
<DataGridComboBoxColumn Header="Line Manager" Width="*"
SelectedValueBinding="{Binding linemanager.ID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="ID"
DisplayMemberPath="Name"
>
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.alllinemanagers}"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.alllinemanagers}"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
</DataGrid.Columns>
答案 0 :(得分:1)
您需要在ViewModel上为selectedclientrep创建一个属性。
private ClientSurveyor selectedclientrep;
public ClientSurveyor SelectedClientRep
{
get { return selectedclientrep; }
set
{
selectedclientrep = value;
OnPropertyChanged("SelectedClientRep");
}
}
您还需要将View上的SelectedItem绑定到新属性SelectedClientRep。
<DataGrid Margin="5" Grid.ColumnSpan="4" ItemsSource="{Binding Path=allclientreps}" SelectedItem="{Binding Path=SelectedClientRep}" AutoGenerateColumns="False">
我希望这会有所帮助。