I am using a radGridView in Master/Detail scenario using MVVM; the selected item is displayed in a custom form that allows editing.
The Form's DataContext and the Grid's SelectedItem are bound to a property in the ViewModel.
When the Grid's selection is changing, I check if the Form is in Edit mode, and if so I cancel the selection change.
This works as it should.
However, there are some circumstances where the Grid's ItemSource (CollectionViewSource) changes and the Grid looses track of the SelectedItem. I can throw NotifyPropertyChanged on the property, and the grid doesn't notice. When the grid is in this state and the user selects a row, the logic in SelectionChanging does not prevent the SelectedItem from changing. In fact it sets the SelectedItem to null, rather than the attempted selection row.
I really need to prevent the SelectedItem from changing (or becoming null) while the item is being edited. I think the best way would be to prevent the grid from getting into this strange state.
How can I force the grid to realize its SelectedItem, if NotifyPropertyChanged is not doing the trick?
Does the grid loose its SelectedItem binding when the Itemssource updates?
XAML:
<telerik:RadGridView Grid.RowSpan="2" ShowGroupPanel="False" AutoGenerateColumns="False" IsFilteringAllowed="False"
ItemsSource="{Binding Zones.View}" x:Name="grd_ZoneBins" IsReadOnly="True" EditTriggers="None"
SelectedItem="{Binding SelectedZone, Mode=TwoWay}"
SelectionChanging="grd_ZoneBins_SelectionChanging" >
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="Zone" DataMemberBinding="{Binding Name}" Width="*" />
<telerik:GridViewDataColumn Header="Active" DataMemberBinding="{Binding IsActive}" />
</telerik:RadGridView.Columns>
</telerik:RadGridView>
CodeBehind:
private void grd_ZoneBins_SelectionChanging(object sender, Telerik.Windows.Controls.SelectionChangingEventArgs e)
{
if (EditableWorkSpace_ZoneBins.Mode == EditMode.Editable) //this is a custom control
e.Cancel = true;
}
ViewModel:
//Properties
private List<Part_Zone> _zones;
public CollectionViewSource Zones { get; set; }
private Part_Zone m_SelectedZone;
public Part_Zone SelectedZone
{
get { return m_SelectedZone; }
set { m_SelectedZone = value; NotifyPropertyChanged("SelectedZone");}
}
public Model() //constructor
{
Zones = new CollectionViewSource();
Zones.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
LoadData();
}
private void LoadData()
{
_zones = GetZones();
Zones.Source = _zones;
SelectedZone = Zones.View.CurrentItem as Part_Zone; //first item
}
public void Refresh()
{
LoadData();
//at this point the grid doesn't seem aware that the SelectedZone is its SelectedItem
}
public void StartEdit()
{
View.EditableWorkSpace_ZoneBins.Mode = EditMode.Editable;
NotifyPropertyChanged("SelectedZone"); // just in case
}
Steps to reproduce:
1) load data
2) select item in grid
3) refresh data - grid looses selectedItem. SelectedZone is not null.
4) start edit
5) change selection - gets canceled due to Edit mode
6) SelectedZone is set to null.