How can I stretch ExpandableFieldRecordPresenter vertically such that it will occupy whole width of XamDataGrid.
I was unable to find any property in XamDataGrid but by using Snoop utility I've found that I need to set HorizontalAlignment=Stretch on ExpandableFieldRecordPresenter and on ancestor element named PART_NestedContentSite, on the latter I also had to set Grid.ColumnSpan=3.
I am currently using attached property with changed handler that walks visual tree and does what is needed but it's a hack.
Code of my hack:
public static readonly DependencyProperty NestedContentHorizontalAlignmentProperty = DependencyProperty.RegisterAttached("NestedContentHorizontalAlignment", typeof (HorizontalAlignment), typeof (XamDataGridHelper), new PropertyMetadata(default(HorizontalAlignment), OnNestedContentHorizontalAlignmentChanged)); private static void OnNestedContentHorizontalAlignmentChanged(DependencyObject d, DependencyPropertyChangedEventArgs ev) { var presenter = (ExpandableFieldRecordPresenter) d; var newValue = (HorizontalAlignment) ev.NewValue; presenter.SetCurrentValue(FrameworkElement.HorizontalAlignmentProperty, newValue); var parent = VisualTreeHelper.GetParent(d); while (parent != null) { var parentFE = parent as FrameworkElement; if (parentFE != null && parentFE.Name == "PART_NestedContentSite") { // XXX this will break if style will change significantly var grid = VisualTreeHelper.GetParent(parentFE) as Grid; Debug.Assert(grid != null, "Incompatible style template of XamDataGrid"); if (grid != null) { parentFE.SetCurrentValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Stretch); parentFE.SetCurrentValue(Grid.ColumnSpanProperty, 3); grid.Dispatcher.BeginInvoke(new Action(grid.InvalidateMeasure), DispatcherPriority.Background); } return; } parent = VisualTreeHelper.GetParent(parent); } Debug.Fail("Could not set NestedContentHorizontalAlignment on XamDataGrid, incompatible style template"); } public static void SetNestedContentHorizontalAlignment(ExpandableFieldRecordPresenter element, HorizontalAlignment value) { element.SetValue(NestedContentHorizontalAlignmentProperty, value); } public static HorizontalAlignment GetNestedContentHorizontalAlignment(ExpandableFieldRecordPresenter element) { return (HorizontalAlignment)element.GetValue(NestedContentHorizontalAlignmentProperty); }
Hi,
Instead of using code behind and the visual tree helper the other approach is to re-template the style. In this case you would copy the ExpandableFieldRecordPresenter style from the DataPresenterGeneric_Express.xaml file in the DefaultStyles folder into your application and make the modifications you need there.
Let me know if you have any questions.