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

隐身守侯 提交于 2019-12-10 01:33:48

问题


It seems that it is not possible to bind the visibility property of a DataGridTemplateColumn in Silverlight 4 still. I did some Googling and there seem to be a few posts suggesting it was to do with the fact that it was not a DependencyObject and how this would change in SL4 but it does not seem to be the case.

To work around it, I do it in the code behind of the datagrid loaded event, but I am curious as to why this is the case?

Here is the error message I get (with a converter that returns a Visibility value):

{System.ArgumentException: Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.Windows.Visibility'.
   at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
   at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
   at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
   at MS.Internal.XamlMemberInfo.SetValue(Object target, Object value)
   at MS.Internal.XamlManagedRuntimeRPInvokes.SetValue(XamlTypeToken inType, XamlQualifiedObject& inObj, XamlPropertyToken inProperty, XamlQualifiedObject& inValue)}

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/3433396/why-can-i-not-bind-the-visiblity-of-a-datagridtemplatecolumn-in-silverlight-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!