Hello!
I need a way to initialize FlatDataSource from code. I use MVVM pattern and currently FlatDataSource in my ViewModel is populated with initial items and initialized only after view have been initialized. Now if I want to do something with initialized datasource, I do this in ResultChanged event handler. But this breaks View-ViewModel separation and I can't properly test my code.
Maybe I'm missing something? How can I initialize FlatDataSource with items from ItemsSource without loading the PivotGrid?
Here is my code to generate FlatDataSource:
DataSource = new FlatDataSource { ItemsSource = Items, Cube = DataSourceBase.GenerateInitialCube("DistributorInventory"), Rows = DataSourceBase.GenerateInitialItems("[Distributor].[Distributor]"), Columns = DataSourceBase.GenerateInitialItems("[Date].[Date]"), Measures = DataSourceBase.GenerateInitialItems("Value") };
var cubeMetadata = new CubeMetadata { DisplayName = "Cube", DataTypeFullName = typeof(DistributorInventory).FullName }; cubeMetadata.DimensionSettings.Add(new DimensionMetadata { SourcePropertyName = "Value", DisplayName = "Value", });DataSource.CubesSettings.Add(cubeMetadata);
var datesHierarchy = new HierarchyDescriptor(p => p.Date) {HierarchyDisplayName = "Date"}; datesHierarchy.AddLevel(p => p.MonthText, "Month"); datesHierarchy.AddLevel(p => p.WeekText, "Week"); DataSource.AddHierarchyDescriptor(datesHierarchy);
var itemsHierarchy = new HierarchyDescriptor(p => p.Distributor) {HierarchyDisplayName = "Distributor"}; itemsHierarchy.AddLevel(p => p.Distributor, "Distributor"); itemsHierarchy.AddLevel(p => p.Address, "Address"); itemsHierarchy.AddLevel(p => p.Item, "Item");DataSource.AddHierarchyDescriptor(itemsHierarchy);
DataSource.ResultChanged += OnDataSourceOnResultChanged;
Thank you for your answer!
Of course I have done this! I just wanted to keep post simple. I initialized FlatDataSource exactly like in my first post in this topic. My code in last post is a continuation of code in my first post.
Hello,
I looked at your code again and I think you might need to add the metadata settings first and then set the items source.
Regards,Plamen
Hello again!
I'm sorry, but it still isn't working. I tested initializing the DataSource without PivotGrid control by removing control from the view, but now I'm creating unit tests with the data and it's not working.
ResultChanged event is not firing now! I've tried setting a breakpoint to DataSourceBase.RunWorkerCompleted (the method from where OnResultChanged is called when everything works fine), but the breakpoint is never in my unit test!
That's the code:
DataSource.ResultChanged += OnDataSourceOnResultChanged; DataSource.LoadSchemaAsync();
private void OnDataSourceOnResultChanged(object sender, ResultChangedEventArgs args) {
// never gets here
if (DataSource.Result == null || DataSource.Result.ColumnAxis == null || DataSource.Result.ColumnAxis.Tuples.Count == 0 || DataSource.Result.ColumnAxis.Tuples[0].Members.Count == 0 || DataSource.Result.ColumnAxis.Tuples[0].Members[0].UniqueName == String.Empty) return;
// ... Here goes conditions checking, but it never gets here...
DataSource.ResultChanged -= OnDataSourceOnResultChanged; }
Thank you for your feedback. I am glad that you resolved your issue and I believe that other community members may benefit from this as well.
Thanks again.
This works, but now I have two OnResultChanged calls: I think that one is from LoadSchemeAsync, and other is from XamPivotGrid. I solved this by checking DataSource.Result.ColumnAxis.Tuples[0].Members[0].UniqueName == String.Empty.