My data columns are dynamic so they change based on the query parameters. The column number and/or column labels can change. They way I implemented this is by using the FieldLayoutInitialized event in the code behind.
From doing some reading the FieldLayoutInitialized event will not fire if the column types and column number is the same. The problem I'm having is this:
If a user picks calendar year for the query date range and enters in say 2011 to 2012 the column labels are' 2011' and '2012' but if they pick fiscal year the column labels change to '2011/2012' and '2012/2013' but this does not get updated in the xamdatagrid because the FieldLayoutInitialized event does not fire because I'm assuming the number and type of columns are the same as the previous field layout.
Is there a way to force the FieldLayoutInitialized event to fire every time so my field labels get updated?
Hi KrisVice,
You can setup an attached property and bind to a property in your viewmodel. Just before you update the datasource in the viewmodel, set this property so that it triggers the attached property. Inside the attached property it can clear the layouts. Like this:
public class DataGridBehaviors { public static bool GetClearFieldLayouts(DependencyObject obj) { return (bool)obj.GetValue(ClearFieldLayoutsProperty); } public static void SetClearFieldLayouts(DependencyObject obj, bool value) { obj.SetValue(ClearFieldLayoutsProperty, value); } // Using a DependencyProperty as the backing store for ClearFieldLayouts. This enables animation, styling, binding, etc... public static readonly DependencyProperty ClearFieldLayoutsProperty = DependencyProperty.RegisterAttached("ClearFieldLayouts", typeof(bool), typeof(DataGridBehaviors), new PropertyMetadata(false, OnClearFieldLayoutsChanged)); private static void OnClearFieldLayoutsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var dataGrid = d as XamDataGrid; var result = (bool)e.NewValue; if (result) { dataGrid.FieldLayouts.Clear(); SetClearFieldLayouts(dataGrid, false); } } }
Here is the viewmodel property:
private bool _clearFieldLayouts; public bool ClearFieldLayouts { get { return _clearFieldLayouts; } set { if (value) { _clearFieldLayouts = true; OnPropertyChanged("ClearFieldLayouts"); _clearFieldLayouts = false; } } }
Then just bind it up:
<igDP:XamDataGrid ... local:DataGridBehaviors.ClearFieldLayouts="{Binding Path=ClearFieldLayouts, Mode=TwoWay}">
How might I accomplish this? I'm using the MVVM pattern. I can't bind to FieldLayouts property because it's read only. How would I clear the FieldLayouts collection in the view model?
If you are assigning a new DataSource when the query is changed then what you can do is clear out the FieldLayouts collection before assigning the new DataSource. This way when the new DataSource is assigned it won't find any FieldLayouts that match and it will create a new one triggering that event.