Strange behavior of DataBinding to Grouped GridView
-
Thursday, April 05, 2012 8:58 PM
I have problem with adding new group to Grouped GridView. I do that using code below (it is simplified).
public sealed partial class HomePage : Page
{
this._groups = new ObservableCollection<Group>();
public HomePage()
{
this.InitializeComponent();
TestData data = new TestData();
data.Symbol = "BBB";
data.Type = "Akcie";
data.Name = "SAS";
TestUnit unit = new TestUnit();
unit.SharePrice = 10;
unit.Shares = 100;
data.history.Add(unit);
GroupCollection group = new GroupCollection(); //custom ObservableCollection
group.Key = "Akcie";
group.Add(data);
this._groups.Add(group);
CollectionViewSource dataSource = new CollectionViewSource();
dataSource.Source = this._groups;
dataSource.IsSourceGrouped = true;
this.GridViewTest.DataContext = dataSource;
}
}When i want to add some elements lately i can use this method (for example):
public async void AddGroup()
{
TestData data = new TestData();
data.Symbol = "ASD";
data.Type = "AAFDSGF";
data.Name = "ADS";
data.IsFirst = true;
TestUnit unit = new TestUnit();
unit.SharePrice = 10;
unit.Shares = 100;
data.history.Add(unit);
GroupCollection group = new GroupCollection();
test.Key = "ASDSAD";
test.Add(data);
this._groups.Add(test);
}When i call this method at the end of constructor or from other method that is imidiately executed method (called from constructor) then the Group is added correctly (header and items). But when i call this method in handler of click event then in GridView is visible only new Header but items arent visible. When i checked variables i see that items are set..
Any ideas?
All Replies
-
Thursday, April 05, 2012 10:38 PM
The Metro version of CVS doesn't implement CollectionChanged the way the WPF version does, so you may have to reset it manually.
There's an old thread on the subject here: http://msdn.microsoft.com/en-us/library/windows/apps/Hh758288.aspx I haven't had a chance to re-test in the CP, so I'm not sure what, if anything has changed.
Rebecca M. Riordan
-
Saturday, April 07, 2012 3:41 PMDo you know about any way to reset it manully? The link you provide is propably wrong (it isn't thread).
-
Sunday, April 08, 2012 12:40 PM
Oh, sorry. The thread is here: http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/aec84969-0947-4fe8-bcf6-ab533e7ab894
I meant to re-assign the source after the update:
this.GridViewTest.DataContext = dataSource;
If that doesn't work, you can try calling ClearValue() first, but I doubt that will be necessary.
Be careful about maintaining the current item if that's important in your situation. I don't know what the CVS is going to do with that when you add a new group. (If you do some testing, will you let me know what your results are, please?)
Rebecca M. Riordan
-
Monday, April 09, 2012 10:58 AM
I suspended my work on this because i was unable to make this work in the way which i wanted.
When i try to add a new group to CVS then header of that group will show correctly. But items from that group did'nt show.
I think that now is CVS and grouped GridView usable only when you dont want add or remove content dynamicaly. Changes of items attributes in groups works correctly when that item implements INotifyPropertyChanged.


