How to get the Logical Children of a RowDefinition?

595 Views Asked by At

I have Extended the RowDefinition as RowDefinitionExtended and In that, when can i get the LogicalChildren belongs to this RowDefinition. I mean in which override can i get the LogicalChildren?

public class RowDefinitionExtended : RowDefinition
{
    protected override void OnInitialized(EventArgs e)
    {
        base.OnInitialized(e);
        Loaded += OnRowDefinitionExtendedLoaded;
    }


    void OnRowDefinitionExtendedLoaded(object sender, RoutedEventArgs e)
    {
        var parent = GetUIParentCore() as Grid;
        if (parent == null) return;

        if (parent.Children.Cast<UIElement>().Where(c => Grid.GetRow(c) == parent.RowDefinitions.IndexOf(this)).All(ctrl => ctrl.Visibility != Visibility.Visible))
            Height = new GridLength(0);
    }

} 

What my requirement is, I need to check all the LogicalChildren to its Visibility and Change its Height accordingly.

How could i do this? Any idea?

Update:

Code has been updated, On Load I could do this and it works fine. But my problem is, am changing the controls visibility after load... So is there any notification while changing the Visibility? am looking a event when the layout updated like..

Any event can i use it for?

2

There are 2 best solutions below

2
On

You have to subscribe to the IsVisibleChanged handler for each element in the row when the row is loaded.

When the visibility changed, you could do whatever you need

 public class RowDefinitionExtended : RowDefinition
    {
        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            Loaded += OnRowDefinitionExtendedLoaded;
        }


        void OnRowDefinitionExtendedLoaded(object sender, RoutedEventArgs e)
        {
            var parent = GetUIParentCore() as Grid;
            if (parent == null) return;

            //Subscribe to the IsVisibleChanged handler for each element in the row
            var ElementInGridRow = parent.Children.Cast<UIElement>().Where(c => Grid.GetRow(c) == parent.RowDefinitions.IndexOf(this));
            foreach (var element in ElementInGridRow)
            {
                element.IsVisibleChanged+=new DependencyPropertyChangedEventHandler(OnChildrenIsVisibleChanged);
            }
        }

        private void OnChildrenIsVisibleChanged(object sender,DependencyPropertyChangedEventArgs e)
        {
            UIElement element = sender as UIElement;

            //Do stuff...
            var parent = GetUIParentCore() as Grid;
            if (parent.Children.Cast<UIElement>().Where(c => Grid.GetRow(c) == parent.RowDefinitions.IndexOf(this)).All(ctrl => ctrl.Visibility != Visibility.Visible))
                Height = new GridLength(0);
        }

    } 
0
On

You can't do that by means of a derived RowDefinition, but this little helper method should do the job (if your intention was to get all child elements in a certain row of a Grid):

public static IEnumerable<UIElement> ChildrenInRow(Grid grid, int row)
{
    return grid.Children.Cast<UIElement>().Where(c => Grid.GetRow(c) == row);
}