0

I have an Modernui Piechart binded to an ObservableCollection.

If I change the name of an item it is not updating, but if I change the value of on item then it will be updated properly(add/remove works fine).

Chart XAML :

<chart:PieChart Grid.RowSpan="2"
    Style="{StaticResource MinimalChartStyle}"
    ChartTitle="Minimal Pie Chart"
    ChartSubTitle="Chart with fixed width and height"
     >
                <chart:PieChart.Series>
                    <chart:ChartSeries
            SeriesTitle="Categories"
            DisplayMember="CategoryName"
            ValueMember="CategoryExpenseLimit"
            ItemsSource="{Binding Path=Cat}" />
                </chart:PieChart.Series>
            </chart:PieChart>

Code where I add item: (this updates the chart properly when adding item)

TransactionCategoryModel category = new TransactionCategoryModel() { TheCategory = { CategoryName = CategoryName, CategoryExpenseLimit = (decimal)CategoryExpenseLimit }};

                        context.TransactionCategories.Add(category.TheCategory);
                        context.SaveChanges();
                        var obs = Application.Current.Resources["CategoryObs"] as ObservableCollection<CategoryViewModel>;
                        obs.Add(new CategoryViewModel(category));

Code to edit an Item :(retrieve it from the database and update it then update the observable collection as well)

                var category = context.TransactionCategories.Where(i => i.CategoryId == this.CategoryId).First();
                var tCategory = new TransactionCategoryModel() { TheCategory = category };

                    tCategory.TheCategory.CategoryId = (int)CategoryId;
                    tCategory.TheCategory.CategoryName = CategoryName;
                    tCategory.TheCategory.CategoryExpenseLimit = (decimal)CategoryExpenseLimit;
                    context.SaveChanges();
                    var obs = Application.Current.Resources["CategoryObs"] as ObservableCollection<CategoryViewModel>;
                    var x = obs.Where(i => i.CategoryId == this.CategoryId).FirstOrDefault();
                    CategoryViewModel cvm = new CategoryViewModel(tCategory);

                    x = cvm;

With this I edit an item. Problem is if I edit it and change the name the chart does not update the displaymember, but if I also change the expenselimit(this is the valuemember of the chart) then the chart will update properly. The fact that the name does not update happens only with the chart. I made a datagrid in another view and binded the Observablecollection and the data grid updates properly even when I change only the name.

In the ViewModel of the chart :

 private ObservableCollection<CategoryViewModel> cat;
    public ObservableCollection<CategoryViewModel> Cat
    {
        get { return cat; }
        set
        {
            cat = value;
            OnPropertyChanged("Cat");
        }

    }

And in the constructor:

if (cat == null)
            cat = new ObservableCollection<CategoryViewModel>();
        cat = Application.Current.Resources["CategoryObs"] as ObservableCollection<CategoryViewModel>;

And when I the app starts : to retrieve the values

 private void GetCategories()
    {
        List<CategoryViewModel> categories = new List<CategoryViewModel>();

        using( var context = new Ents())
        {
            foreach(var item in context.TransactionCategories)
            {
                TransactionCategoryModel tcm = new TransactionCategoryModel() { TheCategory = item };
                categories.Add(new CategoryViewModel(tcm));
            }
        }
        ObservableCollection<CategoryViewModel> Categories = new ObservableCollection<CategoryViewModel>(categories);
        Application.Current.Resources.Add("CategoryObs", Categories);
    }
Cristian
  • 275
  • 4
  • 17

1 Answers1

0

..edit it and change the name the chart does not update the displaymember

Two things, an ObservableCollection is primarily a signaler for when items are added or removed from its list. It doesn't help binding or processing of items which are edited.

Secondly for any changes of individual items to be noticed, the property which it is bound needs to signal its change. That signaling is done when the class it resides on implements INotifyPropertyChange and it calls an OnPropertyChanged method with its name.


So looking at your code, you are learning I get that, the code

public ObservableCollection<CategoryViewModel> Cat {
        get { return cat; }
        set { cat = value; OnPropertyChanged("Cat"); }}

Only signals when a new observable collection is assigned. Now that may be helpful in certain situations, such as swapping in and out of lists (I do it to change combobox drop down selections) but in your case its a one trick pony which is useful to the chart and needed, but the chart doesn't care you have an ObservableCollection. Because one has to purposefully subscribe to the ObservableCollection events to do anything. Frankly you could and should just use a List<CategoryViewModel> instead.

chart does not update the displaymember

Does the property CategoryName on the class CategoryViewModel call an PropertyChange because CategoryViewModel implements INotifyPropertyChanged?

Note...even it if does, the charting tool may not be subscribing to the change notification, because its a reporting tool and wasn't designed as such. If that is the case, you may need to wink the whole CategoryViewModel instance in and out (remove it then re-add it) to make the reporting chart control show the change in the name.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • I tried removing and adding the item but since I'm removing and adding the item it shifts the 'pies'. I'm using a collectionview since I want to add filtering later and made a refresh button. Marking it as answer since it would be a solution. – Cristian Apr 30 '15 at 07:41
  • @Cristian Each control is different and you had the overall architecture so it had to be something missing. Glad you got it working as such. – ΩmegaMan Apr 30 '15 at 12:54