Why can I not bind the Visiblity of a DataGridTemplateColumn in Silverlight 4?

柔情痞子 提交于 2019-12-05 01:01:51

Whilst the DataGridTemplateColumn does derive from DependencyObject it does not define a DependencyProperty for its Visibility property. In fact it does not define any dependency properties hence you still can't get anything to bind on it.

Use this for any properties that you want to bind to, on the Data Grid Template Column:

public class CustomDataGridTemplateColumn : DataGridTemplateColumn
{
    public static readonly DependencyProperty VisibilityBindingProperty = DependencyProperty.Register(
      "VisibilityBinding", typeof(Visibility), typeof(CustomDataGridTemplateColumn), new PropertyMetadata(Visibility.Collapsed, new PropertyChangedCallback(OnVisibilityChanged)));

    public Visibility VisibilityBinding
    {
        get { return (Visibility)this.GetValue(VisibilityBindingProperty); }
        set { this.SetValue(VisibilityBindingProperty, value); }
    }

    private static void OnVisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((CustomDataGridTemplateColumn)d).Visibility = (Visibility)e.NewValue;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!