I have a software (with GUI) containing metainformation about products. Part of that information are some ratings of components of that product.
I want to display this ratings somehow like this for everey product someone clicked on:
Part a: 100%
Part b: 20%
Part c: -
Part d: 40%
It can be lots of values and i don't want to create an own variable for every possible rating.
My problem is that i have lots of missing values but i want the GUI allways to look the same so i thought of an observable dictionary to store my data (coming from a webinterface on the fly for the product someone clicks on) and bind wpf elements to the values of that dictionary.
I found an implementation of an ObservableDictionary in this stackoverflow post which contains a link to the Implementation.
Here are some parts of my code to show where my problem is:
My View implementation looks like this:
<TextBlock Text="{Binding Path=Ratings, Converter={StaticResource ObservableDictionaryConverter}, ConverterParameter=Key}"/>
Where Ratings is my Observable Dictionary, Key is my hardcoded Component (e.g. "Part a") and ObservableDictionaryConverter is a converter returning the rating if it exists or "-" if it doesn't.
My ModelView Implementation looks like this
public class Foo : Bindable
{
ObservableDictionary<string, ComponentRatings> _ratings
public ObservableDictionary<string, ComponentRatings> Ratings
{
get {return _ratings; }
set
{
_ratings = value;
OnPropertyChanged();
}
}
}
And the Model:
ViewModel.Ratings= new ObservableDictionary<string, ComponentRatings>();
foreach (var e in foo)
{
ViewModel.Ratings.Add(e.Drive.ToString(), e);
}
1.) If i run my code the gui only updates itself when the Dictionary is instantiated due to the OnPropertyChanged() in my ModelView property. But when i Add new Elements to the Dict, my GUI should update itself and show them. So how do i catch the CollectionChanged events from the Observable Dictionary in my ModelView?
2.) The hardcoded Keys in the XAML are not really nice but i couldn't come up with a better way to allways have all my ratings in the GUI even if they don't exist. Is there some better, more flexible way to implement something like this?