2010-08-08 88 views
11

看來,仍然無法綁定Silverlight 4中DataGridTemplateColumn的visibility屬性。我做了一些谷歌搜索,似乎有幾個帖子暗示它要做with the fact that it was not a DependencyObject以及this would change in SL4,但似乎並非如此。爲什麼我不能在Silverlight 4中綁定DataGridTemplateColumn的Visiblity?

爲了解決這個問題,我在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)} 

回答

12

雖然DataGridTemplateColumn並從DependencyObject派生它並沒有爲它的可見性屬性DependencyProperty。事實上它沒有定義任何依賴屬性,因此你仍然無法獲得任何東西來綁定它。

+0

感謝您的幫助一如既往安東尼。 – Rodney 2010-08-09 00:36:39

+0

太令人沮喪。 – 2011-05-23 23:08:00

+0

同意。 Hopefull MS將來會製造這些依賴屬性。 – 2011-05-30 01:01:52

7

使用此爲您要綁定到任何屬性,在數據網格模板欄目:

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

謝謝你提供了一個非常好的解決方法。 :) – 2012-09-25 11:09:32

+1

嘿@JohnySkovdal和StuartBale:這看起來不錯,但我試圖使用這個(失敗),並想知道是否有其他東西我失蹤,例如。 XAML中定義的綁定有什麼特別需要?你是綁定到ViewModel還是行/ ItemsSource的DataContext? – 2012-10-24 19:36:16

+0

@JoeL。我實際上最終走上了另一條路線,因爲我也遇到了很多問題,但我不能100%確定它們與此解決方案相關,所以我無法真正幫助您。我的問題比較複雜,所以我最終在後面的代碼中創建了模板,如果不應該顯示,則不添加列。 – 2012-10-25 07:02:40

相關問題