代码之家  ›  专栏  ›  技术社区  ›  Rodney

为什么我不能在Silverlight4中绑定DataGridTemplateColumn的可见性?

  •  11
  • Rodney  · 技术社区  · 14 年前

    似乎无法在Silverlight4中绑定DataGridTemplateColumn的可见性属性。我在谷歌上搜索了一下,好像有几个帖子暗示我要这么做。 with the fact that it was not a DependencyObject 以及如何 this would change in SL4 但情况似乎并非如此。

    为了解决这个问题,我在DataGrid加载事件后面的代码中做了这个工作,但是我很好奇为什么会这样?

    以下是我得到的错误消息(使用返回可见性值的转换器):

    {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)}
    
    2 回复  |  直到 12 年前
        1
  •  12
  •   AnthonyWJones    14 年前

    同时 DataGridTemplateColumn 是否源自 DependencyObject 它没有定义 DependencyProperty 因为它的可见性属性。实际上,它没有定义任何依赖属性,因此您仍然无法在其上绑定任何内容。

        2
  •  7
  •   Stuart Bale    12 年前

    对于要绑定到的任何属性,请在“数据网格模板”列上使用此选项:

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