WPF: How to hide GridViewColumn using XAML?

后端 未结 10 654
北海茫月
北海茫月 2020-12-05 09:59

I have the following object in App.xaml


        
            

        
10条回答
  •  北海茫月
    2020-12-05 10:46

    Actually, I find the easiest solution is via attached properties:

    public class GridViewColumnVisibilityManager
    {       
        static void UpdateListView(ListView lv)
        {
            GridView gridview = lv.View as GridView;
            if (gridview == null || gridview.Columns == null) return;
            List toRemove = new List();
            foreach (GridViewColumn gc in gridview.Columns)
            {
                if (GetIsVisible(gc) == false)
                {
                    toRemove.Add(gc);
                }
            }
            foreach (GridViewColumn gc in toRemove)
            {
                gridview.Columns.Remove(gc);
            }
        }
    
        public static bool GetIsVisible(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsVisibleProperty);
        }
    
        public static void SetIsVisible(DependencyObject obj, bool value)
        {
            obj.SetValue(IsVisibleProperty, value);
        }
    
        public static readonly DependencyProperty IsVisibleProperty =
            DependencyProperty.RegisterAttached("IsVisible", typeof(bool), typeof(GridViewColumnVisibilityManager), new UIPropertyMetadata(true));
    
    
        public static bool GetEnabled(DependencyObject obj)
        {
            return (bool)obj.GetValue(EnabledProperty);
        }
    
        public static void SetEnabled(DependencyObject obj, bool value)
        {
            obj.SetValue(EnabledProperty, value);
        }
    
        public static readonly DependencyProperty EnabledProperty =
            DependencyProperty.RegisterAttached("Enabled", typeof(bool), typeof(GridViewColumnVisibilityManager), new UIPropertyMetadata(false,
                new PropertyChangedCallback(OnEnabledChanged)));
    
            private static void OnEnabledChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            ListView view = obj as ListView;
            if (view != null)
            {
                bool enabled = (bool)e.NewValue;
                if (enabled)
                {
                    view.Loaded += (sender, e2) =>
                    {
                        UpdateListView((ListView)sender);
                    };
                    view.TargetUpdated += (sender, e2) =>
                    {
                        UpdateListView((ListView)sender);
                    };
                    view.DataContextChanged += (sender, e2) =>
                    {
                        UpdateListView((ListView)sender);
                    };
                }
            }
        }
    }
    

    Then, it can be used as so:

    
    ...
    
                            
                                 ...
    

提交回复
热议问题