11

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)}
Rodney
  • 5,417
  • 7
  • 54
  • 98

2 Answers2

12

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.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
  • Thanks for your help as always Anthony. – Rodney Aug 09 '10 at 00:36
  • Agreed. Hopefull MS makes these dependency properties in the future. – Matt Ruwe May 30 '11 at 01:01
  • You can force the datacontext to trigger a dependency property change by using the [`DataGridContextHelper`](http://blogs.msdn.com/b/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx) static class. – SliverNinja - MSFT Jan 16 '12 at 21:32
7

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;
    }
}
Stuart Bale
  • 71
  • 1
  • 1
  • 1
    Hey @JohnySkovdal and StuartBale: This looks great, but I'm trying to use this (unsuccessfully) and was wondering if there was something else I'm missing, eg. is there anything special needed in the Binding defined in the XAML? Are you binding to the ViewModel, or the DataContext of the row/ItemsSource? – Joe L. Oct 24 '12 at 19:36
  • @JoeL. I actually ended up going another route, as I too ran into a lot of issues, but I'm not 100% sure they were related to this solution though, so I can't really help you there. My problem was quite a bit more complex, so I ended up creating the templates in code behind, and then not add the column if it should not be shown. – Johny Skovdal Oct 25 '12 at 07:02
  • Thanks! Thats a great solution – Ichibanpanda Jun 01 '15 at 21:50