Binding to Children.Count

后端 未结 2 1042
名媛妹妹
名媛妹妹 2021-01-20 01:01

I\'m using a binding of the form

{Binding RelativeSource={RelativeSource Self}, Path=Children.Count, Converter={StaticResource CountToDimensionConverter}, C         


        
2条回答
  •  [愿得一人]
    2021-01-20 01:19

    public class GridEx : Grid, INotifyPropertyChanged
    {
        public delegate void ChildrenUpdatedEventHandler(DependencyObject visualAdded, DependencyObject visualRemoved);
    
        public event ChildrenUpdatedEventHandler ChildrenUpdated;
        public event PropertyChangedEventHandler PropertyChanged;
    
        public bool HasChildren => Children.Cast().Where(element => element != null).Count() > 0;
        public int ChildCount => Children.Cast().Where(element => element != null).Count();
    
        protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved)
        {
            base.OnVisualChildrenChanged(visualAdded, visualRemoved);
    
            ChildrenUpdated?.Invoke(visualAdded, visualRemoved);
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(HasChildren)));
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ChildCount)));
        }
    }
    

    Use property ChildCount instead.

提交回复
热议问题