How to bind DataGridColumn.Visibility?

前端 未结 9 1941
悲哀的现实
悲哀的现实 2020-12-10 03:46

I have an issue similar to the following post:

Silverlight DataGridTextColumn Binding Visibility

I need to have a Column within a Silverlight DataGrid be vis

9条回答
  •  孤城傲影
    2020-12-10 03:50

    I have another solution to this problem that uses an approach similar to the "Binding" property that you find on DataGridTextColumn. Since the column classes are DependencyObjects, you can't directly databind to them, BUT if you add a reference to a FrameworkElement that implements INotifyPropertyChanged you can pass a databinding through to the element, and then use a dependency property to notify the Column that the databinding has changed.

    One thing to note is that having the binding on the Column itself instead of the Grid will probably mean that you will want to use a DataContextProxy to get access to the field that you want to bind the Visibility to (the column binding will default to the scope of the ItemSource).

    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    
    namespace XYZ.Controls
    {
    public class ExtendedDataGridTextColumn : DataGridTextColumn
    {
        private readonly Notifier _e;
    
        private Binding _visibilityBinding;
        public Binding VisibilityBinding
        {
            get { return _visibilityBinding; }
            set
            {
                _visibilityBinding = value;
                _e.SetBinding(Notifier.MyVisibilityProperty, _visibilityBinding);
            }
        }
    
        public ExtendedDataGridTextColumn()
        {
            _e = new Notifier();
            _e.PropertyChanged += ToggleVisibility;
        }
    
        private void ToggleVisibility(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Visibility")
                this.Visibility = _e.MyVisibility;
        }
    
        //Notifier class is just used to pass the property changed event back to the column container Dependency Object, leaving it as a private inner class for now
        private class Notifier : FrameworkElement, INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            public Visibility MyVisibility
            {
                get { return (Visibility)GetValue(MyVisibilityProperty); }
                private set { SetValue(MyVisibilityProperty, value); }
            }
    
            public static readonly DependencyProperty MyVisibilityProperty = DependencyProperty.Register("MyVisibility", typeof(Visibility), typeof(Notifier), new PropertyMetadata(MyVisibilityChanged));
    
            private static void MyVisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var n = d as Notifier;
                if (n != null)
                {
                    n.MyVisibility = (Visibility) e.NewValue;
                    n.PropertyChanged(n, new PropertyChangedEventArgs("Visibility"));
                }
            }
        }
    }
    

    }

提交回复
热议问题