How to properly implement data binding for a SortedDictionary

36 Views Asked by At

In a WPF application, I am using a DataGrid on my main window. The data that it displays is from a SortedDictionary<int, Individual> , where Individual is a class containing a person’s name and other facts. Specifically, the data comes from the SortedDictionary’s Values property.

Apparently, SortedDictionary does not implement the INotifyCollectionChanged interface so, after having displayed the original set of Values, if I Add or Remove an entry to the dictionary, the UI does not get notified and trouble ensues.

After researching this much on the web, I didn’t get a decent idea of how to go about fixing this so the UI gets properly notified. None of the posts I saw involved a SortedDictionary.

The main features of the SortedDictionary that I use are Add, Remove, and Item[TKey].

Can someone please advise me or point me to an example of how to really do this?

In the XAML for my main window I have:

<DataGrid x:Name="dgPeople" Margin="23" SelectionChanged="dgSelectionChanged" SelectionMode="Single" SelectionUnit="FullRow" AutoGenerateColumns="False" IsReadOnly="True"
                  IsEnabled="False"  TextSearch.TextPath="paternalSurname" MouseDoubleClick="dgPeoplePersonDoubleClicked" Grid.Row="1" MinWidth="1125" MinHeight="435" SizeChanged="dgSizeChanged" 
                  CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeRows="False"
                  AlternationCount="2" AlternatingRowBackground="PeachPuff" IsTextSearchEnabled="True" IsTextSearchCaseSensitive="False">
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="dgtcPaternalSurname" Header="Paternal Surname" Width="150" Binding="{Binding paternalSurname}" />
        <DataGridTextColumn x:Name="dgtcMaternalSurname" Header="Maternal Surname" Width="150" Binding="{Binding maternalSurname}" />
        <DataGridTextColumn x:Name="dgtcNames" Header="Names" Width="360" Binding="{Binding names}" />
        <DataGridTextColumn x:Name="dgtcState" Header="S" Width="17" Binding="{Binding state}" MinWidth="17" MaxWidth="17"/>
        <DataGridTextColumn x:Name="dgtcBirthDate" Header="Birth Date" Width="120" Binding="{Binding birthDate}" MaxWidth="120" MinWidth="120" />
        <DataGridTextColumn x:Name="dgtcBirthPlace" Header="Birth Place" Width="320" Binding="{Binding birthPlace}" MaxWidth="320" MinWidth="320" />
    </DataGrid.Columns>

In the constructor for the window that has the DataGrid I have:

dgPeople.ItemsSource = db.people.Values;
dgPeople.IsTextSearchEnabled = true;
dgPeople.IsTextSearchCaseSensitive = false;

CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(dgPeople.ItemsSource);
view.SortDescriptions.Add(new SortDescription("normalizedPaternal", ListSortDirection.Ascending));
view.SortDescriptions.Add(new SortDescription("normalizedNames", ListSortDirection.Ascending));
0

There are 0 best solutions below