I have a DataGrid grouping problem which I think is related to the (likely incorrect) manner in which I am binding a list of objects to a DataGridComboBoxColumn. I have searched for solutions here and on Google and have not found anything that works.
I have a Category class:
public class Category : INotifyPropertyChanged, IEditableObject { public event PropertyChangedEventHandler PropertyChanged; // Data for undoing canceled edits. private Category temp_Category = null; private bool bEditing = false; private void NotifyPropertyChanged(string name) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(name)); } }//NotifyPropertyChanged private byte _categoryID; public byte ID { get { return _categoryID; } set { _categoryID = value; this.NotifyPropertyChanged("ID"); } } private string _categoryText; public string CategoryText { get { return _categoryText; } set { _categoryText = value.Trim(); this.NotifyPropertyChanged("CategoryText"); } } // Implement IEditableObject interface. public void BeginEdit() { if (this.bEditing == false) { this.temp_Category = this.MemberwiseClone() as Category; this.bEditing = true; } } public void CancelEdit() { if (this.bEditing == true) { this.ID = temp_Category.ID; this.CategoryText = temp_Category.CategoryText; this.bEditing = false; } } public void EndEdit() { if (this.bEditing == true) { this.temp_Category = null; this.bEditing = false; } } }...and a List Categories collection used as the ItemSource for the DataGridComboBoxColumn. The list is part of the application's model and is populated on startup.
I have an ObservableCollection (named ItemsIN) of items each of which has a Category property named "Classification".
The ItemsSource for the DataGrid is set in XAML using a DataContext property in the model:
Code behind:
this.Groups = new ListCollectionView(ItemsIN);
this.Groups.GroupDescriptions.Add(new PropertyGroupDescription("Classification.CategoryText"));
XAML:
<DataGrid AutoGenerateColumns="False" HorizontalAlignment="Stretch"
ItemsSource="{Binding Groups}"
Name="dataGridVariants" VerticalAlignment="Top"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Stretch"
IsSynchronizedWithCurrentItem="{x:Null}"
CanUserAddRows="False"
.... etc. etc., styles, column definitions, etc.
</DataGrid>
Here's the XAML for the DataGridComboBoxColumn:
<DataGridComboBoxColumn.ElementStyle> <Style TargetType="ComboBox"> <Setter Property="ItemsSource" Value="{Binding Path=DataContext.Categories, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/> <Setter Property="IsReadOnly" Value="True"/> <Setter Property="Background" Value="LightGreen"/> <Setter Property="ItemTemplate"> <Setter.Value> <DataTemplate> <TextBlock Text="{Binding Path=Category.CategoryText}"/> </DataTemplate> </Setter.Value> </Setter> </Style> </DataGridComboBoxColumn.ElementStyle> <DataGridComboBoxColumn.EditingElementStyle> <Style TargetType="ComboBox"> <Setter Property="ItemsSource" Value="{Binding Path=DataContext.Categories, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/> <Setter Property="Background" Value="LightGreen"/> <Setter Property="ItemTemplate"> <Setter.Value> <DataTemplate> <TextBlock Text="{Binding Path=Category.CategoryText}"/> </DataTemplate> </Setter.Value> </Setter> </Style> </DataGridComboBoxColumn.EditingElementStyle>The grid loads fine with the DataGridComboBoxColumn items just fine. However, when I change the Category for an item, the grouping fails to reflect the category change. For example, if I have four items shown in category A, and change the category of one item to B, I should get two category groups, one for A and one for B, but I see one group containing three items in category A and one in category B.
If anyone can see my error(s) I would really appreciate it!