0

First excuse my poor english, I'll try my best.

Description The idea is to create a database1 of "foo" where the user can add/remove/edit the objects. (foo contains an unique ID) This database1 is then used later in the program to fill a second type of Collection. Let's call it Database2. Database2 contains element that inherit from one object foo selected by the user. As the user can edit the objects foo, I have the possibility to then send the modification to the Database2 to keep the objects updated. But for that, i have two nested loop. As the databases grow, I'll probably have performance issues. (I don't want to pass the properties of foo by reference (this will be easier) because foo is versioned. The user has the possibility to choose to update the final object or not)

What I want To avoid that, I want the Database1 to be a Dictionary of <int,foo> (int being the unique ID of foo). By using a Dictionary, I think that the process of checking for modification will be faster as I don't need to iterate thought Database1

The Issue I had a first version with ObservableCollection Binded to a datagrid that is working correctly (the user can add/remove/edit the object foo and the changes are reflected properly on the UI). I made all the modification to implement Database1 in a Dictionary and the only issue I'm facing now is that the modifications are not reflected on the UI

The UI is updated only when I change tabs and come back, or when I sort the datagrid by columns but never in realtime (directly after adding an element)

Is there a way to implement an NotifyPropertyChanged or something similar in a dictionnary? how could I do that?

that's the database1
public Dictionary<int, ToolType> DicToolType { get; set; } = new Dictionary<int, ToolType>();

The data context in the user control is set properly

There's the XAML. All bindings are good. Adding a new element is made from another dialog

            <DataGrid ItemsSource="{Binding DicToolType}"
                      AutoGenerateColumns="False"
                      CanUserAddRows="False"
                      CanUserDeleteRows="False"
                      CanUserReorderColumns="False"
                      IsReadOnly="True">
                <DataGrid.Columns>
                    <DataGridTextColumn FontSize="12" Width="60"
                                        Header="T" Binding="{Binding Key}"/>
                    <DataGridTextColumn FontSize="12" Width="100"
                                        Header="Name" Binding="{Binding Value.Name}"/>
                    <DataGridTextColumn FontSize="12" Width="90"
                                        Header="Type" Binding="{Binding Value.Type}"/>
                </DataGrid.Columns>
            </DataGrid>
tstx
  • 151
  • 2
  • 12

1 Answers1

0

After a little bit more of research, I could find a solution.

I created a new class called "ObservableDictionary" implementing INotifyCollectionChanged & IDictionary<TKey, TValue>

public class ObservableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, INotifyCollectionChanged
{
    private Dictionary<TKey, TValue> _dic = new Dictionary<TKey, TValue>();
    #region [INotifyCollectionChanged Implementation]    
    public event NotifyCollectionChangedEventHandler CollectionChanged = delegate { };
    public void OnCollectionChanged(NotifyCollectionChangedAction action)
    {
        if (CollectionChanged != null)
        {
            CollectionChanged(this, new NotifyCollectionChangedEventArgs(action));
        }
    }
    #endregion
    #region [IDictionary Implementation]
//...
    public void Add(TKey key, TValue value)
    {
        _dic.Add(key, value);
        OnCollectionChanged(NotifyCollectionChangedAction.Reset);
    }
//...
    #endregion
}

Now adding a new item is reflected in the UI

Implementing INotifyCollectionChanged interface

https://social.msdn.microsoft.com/Forums/en-US/2ec0c111-0682-49c8-a9d8-ae374328ea2e/bind-observable-dictionary?forum=wpf

tstx
  • 151
  • 2
  • 12