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
335
Stretch ExpandableFieldRecordPresenter vertically.
posted

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.

Parents
No Data
Reply
  • 335
    posted

    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);
    }
Children