Reputation: 11
I have a DataGrid in WPF that is bound to an observable collection. As I need to monitor which specific records are 'dirty' I've specified my data class as INotifiable and am setting the 'IsDirty' property as part of the setter for each field.
My DataGrid has two colums, a text column and a combobox column. When the text column changes the setter for the appropriate property is called - however when the combobox changes the setter for that property is not called - any ideas where I am going wrong?
Edit: I shold point out that the data loads correctly initially, and that I am able to update the combo box in the UI. Also, whenever any changes at all are made to the grid it gets a red border like the binding is failing. However it also gets this when I change the Name column and that works fine so not sure if linked
Edit: I have also tried adding a selection changed event to see if that fires and it does. However the setter stubbornly will not get called and as such my underlying data doesn't get updated
view model:
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>
Upvotes: 1
Views: 263
Reputation: 1885
You need to create a property on your ViewModel for the selectedclientrep.
private ClientSurveyor selectedclientrep;
public ClientSurveyor SelectedClientRep
{
get { return selectedclientrep; }
set
{
selectedclientrep = value;
OnPropertyChanged("SelectedClientRep");
}
}
You also need to bind the SelectedItem on your View to the new property SelectedClientRep.
<DataGrid Margin="5" Grid.ColumnSpan="4" ItemsSource="{Binding Path=allclientreps}" SelectedItem="{Binding Path=SelectedClientRep}" AutoGenerateColumns="False">
I hope that this helps.
Upvotes: 1