【问题标题】:Why can I not bind the Visiblity of a DataGridTemplateColumn in Silverlight 4?为什么我不能在 Silverlight 4 中绑定 DataGridTemplateColumn 的可见性?
【发布时间】:2011-03-26 21:25:27
【问题描述】:

似乎仍然无法在 Silverlight 4 中绑定 DataGridTemplateColumn 的可见性属性。我做了一些谷歌搜索,似乎有一些帖子暗示它应该做with the fact that it was not a DependencyObject 以及如何做this would change in SL4,但似乎并非如此。

为了解决这个问题,我在数据网格加载事件后面的代码中执行此操作,但我很好奇为什么会出现这种情况?

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

{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)}

【问题讨论】:

    标签: silverlight data-binding silverlight-4.0


    【解决方案1】:

    虽然DataGridTemplateColumn 确实派生自DependencyObject,但它没有为其可见性属性定义DependencyProperty。事实上,它没有定义任何依赖属性,因此您仍然无法在其上绑定任何东西。

    【讨论】:

    • 感谢您一如既往的帮助,Anthony。
    • 同意。 Hopefull MS 将来会制作这些依赖属性。
    • 您可以通过使用DataGridContextHelper 静态类来强制数据上下文触发依赖属性更改。
    【解决方案2】:

    在数据网格模板列上将它用于您要绑定的任何属性:

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

    【讨论】:

    • 嘿@JohnySkovdal 和 StuartBale:这看起来很棒,但我正在尝试使用它(不成功)并且想知道是否还有其他我遗漏的东西,例如。 XAML 中定义的 Binding 有什么特别需要的吗?您是绑定到 ViewModel,还是绑定到行/ItemsSource 的 DataContext?
    • @JoeL。实际上我最终选择了另一条路线,因为我也遇到了很多问题,但我不能 100% 确定它们与这个解决方案有关,所以我无法真正帮助你。我的问题有点复杂,所以我最终在后面的代码中创建了模板,如果不应该显示列,则不添加。
    • 谢谢!这是一个很好的解决方案
    猜你喜欢
    • 2011-12-18
    • 1970-01-01
    • 2011-07-17
    • 2012-01-16
    • 2011-01-25
    • 2016-10-06
    • 2010-11-02
    • 2011-11-07
    • 1970-01-01
    相关资源
    最近更新 更多