0

我已经扩展了RowDefinitionasRowDefinitionExtended和 In that,我什么时候可以得到LogicalChildren属于 this的RowDefinition。我的意思是我可以在哪个覆盖中获得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);
    }

} 

我的要求是,我需要检查它的所有内容LogicalChildren并相应地Visibility更改它Height

我怎么能这样做?任何想法?

更新:

代码已更新,在加载时我可以这样做并且效果很好。但我的问题是,加载后正在更改控件的可见性......那么在更改可见性时是否有任何通知?当布局更新时,我正在寻找一个事件......

我可以将它用于任何活动吗?

4

2 回答 2

2

您不能通过派生的 RowDefinition 来做到这一点,但是这个小助手方法应该可以完成这项工作(如果您的意图是获取 a 的某一行中的所有子元素Grid):

public static IEnumerable<UIElement> ChildrenInRow(Grid grid, int row)
{
    return grid.Children.Cast<UIElement>().Where(c => Grid.GetRow(c) == row);
}
于 2014-03-27T11:01:31.143 回答
0

加载行时,您必须为行中的每个元素订阅 IsVisibleChanged 处理程序。

当能见度改变时,你可以做任何你想做的事

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

    } 
于 2014-03-27T11:58:35.507 回答