Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
190
XamPivotGrid with FlatDataSource: Pure MVVM approach rquired
posted

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://es.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;

}


}