Hi,
I have been trying to get XamPivotGrid to bind to FlatDataSource without injecting my viewmodel with a FlatDataSource object (MVVM way). Below two approaches do not work for me and i have come to believe that xampivotgrid has not evolved fully yet. Appreciate any help in getting the xampivotgrid work the MVVM way.
1. http://help.infragistics.com/Doc/WPF/current/CLR4.0/?page=xamPivotGrid_DataBinding_Using_FlatDataSource.html
The sample here uses a viewmodel with a parameterless constructor. My viewmodel is being constructed using unity and contains parameters.
2. Behaviour approach: The below behaviour does not work either. The behaviour is as per the solution mentioned in this post:
http://ko.infragistics.com/community/forums/t/45156.aspx
public class XamPivotDataSelectorBehavior : Behavior<XamPivotDataSelector>{
public Type ModelType{get { return (Type)(GetValue(ModelTypeProperty));}set { SetValue(ModelTypeProperty, value);}}
public static readonly DependencyProperty ModelTypeProperty =DependencyProperty.Register("ModelType", typeof(Type), typeof(XamPivotDataSelectorBehavior));
public IEnumerable DataSource{get { return (IEnumerable)GetValue(DataSourceProperty); }set { SetValue(DataSourceProperty, value); }}
public static readonly DependencyProperty DataSourceProperty =DependencyProperty.Register("DataSource", typeof(IEnumerable), typeof(XamPivotDataSelectorBehavior));
private static void DataSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){if (d is XamPivotDataSelectorBehavior){var obj = d as XamPivotDataSelectorBehavior;if (obj != null){try{var grid = obj.AssociatedObject as XamPivotDataSelector;obj.DataSourceChanged(grid);// point datasource to flat source, no need to rebind
}catch (Exception ex){}}}}
public static readonly DependencyProperty GridDataChangedProperty =DependencyProperty.Register("GridDataChanged", typeof(bool), typeof(XamPivotDataSelectorBehavior), new PropertyMetadata(DataSourceChanged));
public bool GridDataChanged{get { return (bool)GetValue(GridDataChangedProperty); }set { SetValue(GridDataChangedProperty, value); }}
protected virtual void DataSourceChanged(XamPivotDataSelector selector){if (selector.DataSource == null){var dataSource = new FlatDataSource();
selector.DataSource = dataSource;
}
((FlatDataSource)selector.DataSource).ItemsSource = GenerateTypedList(DataSource);
protected virtual IList GenerateTypedList(IEnumerable dataSource){
DynamicTypeBuilder typeBuilder = new DynamicTypeBuilder{DynamicTypeName = ModelType.FullName,DynamicAssemblyName = "TrendsAssembly"};
var dynamicPropertyList = new List<DynamicTypePropertyInfo>();foreach (PropertyInfo propertyInfo in ModelType.GetProperties(BindingFlags.Public)){DynamicTypePropertyInfo dtpi = new DynamicTypePropertyInfo();dtpi.PropertyName = propertyInfo.Name;dtpi.PropertyType = propertyInfo.PropertyType;dynamicPropertyList.Add(dtpi);
var dynamicType = typeBuilder.GenerateType(dynamicPropertyList);
//var type = dataSource.GetType().GetGenericArguments().First();
Type listType = typeof(List<>);Type genericListType = listType.MakeGenericType(dynamicType);var list = (IList)Activator.CreateInstance(genericListType);
foreach (var item in dataSource){
var dynamicInstance = Activator.CreateInstance(dynamicType);
foreach (var pi in dynamicType.GetProperties(BindingFlags.Public)){var modelPI = ModelType.GetProperty(pi.Name);pi.SetValue(dynamicInstance, modelPI.GetValue(item,null),null);
list.Add(dynamicInstance);}
return list;
Hello Amit,
I am just checking if you got this worked out or you still require any assistance or clarification on the matter.
Could you please send us an isolated sample project, where you use your behavior with a test data similar to your original one, so I could investigate this further for you?
Looking forward for your reply.
Hi Stefan,
The ViewModel is created by a DI container (unity) and apparently ObjectDataProvider probably needs compile time resolvable parameters.
The behaviour approach should have worked but i must be missing something. Please could you look into the behaviour approach or suggest how ObjectDataProvider could be used when ViewModel has parameters injected by Unity at runtime ?
Thanks
Amit Kamat
I am just checking the progress of this issue and was wondering if you managed to achieve your goal or if you need any further assistance on the matter.
Thank you for your post. I have been looking into it and I can suggest you use the first approach, but you can use ObejctDataProvider in order to be able to use your parameterized constructor. Here you can read more about this class:
http://msdn.microsoft.com/en-us/library/system.windows.data.objectdataprovider(v=vs.110).aspx
Please let me know if this helps you or you have further questions on this matter.