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);
}