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)}
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.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With