似乎无法在Silverlight 4中绑定DataGridTemplateColumn的visibility属性。我做了一些谷歌搜索,似乎有一些帖子暗示要做with the fact that it was not a DependencyObject以及如何this would change in SL4,但似乎并非如此。
要解决它,我在datagrid加载事件的代码中执行此操作,但我很好奇为什么会出现这种情况?
以下是我收到的错误消息(使用返回Visibility值的转换器):
{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)}
答案 0 :(得分:12)
虽然DataGridTemplateColumn
确实来自DependencyObject
,但它没有为其Visibility属性定义DependencyProperty
。实际上它没有定义任何依赖属性,因此你仍然无法获得任何绑定它。
答案 1 :(得分: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;
}
}