使用 Silverlight DataGrid 绑定到可为 Null 类型的 HasValue
我在使用此绑定时遇到问题:
<sdk:DataGridCheckBoxColumn Header="Invoiced" Binding="{Binding SalesInvoiceId.HasValue}" />
错误是: System.Windows.Data 错误:BindingExpression 路径错误:在“6”“System.Int32”(HashCode=6) 上找不到“HasValue”属性。 BindingExpression: Path='SalesInvoiceId.HasValue' DataItem='Entities.DeliveryNote' (HashCode=5034835);目标元素是“System.Windows.Controls.CheckBox”(名称=“”);目标属性为“IsChecked”(类型“System.Nullable`1[System.Boolean]”)。
字段 SalesInvoiceId 位于该行的数据上下文中,所有其他列都可以正常绑定。
SalesInvoiceId 的类型为int?
。在调试时查看对象时,它显示为 int?
,因此 DataGrid 应该不会有问题 - 但它引用它时就好像它只是一个 int
!
我做错了什么吗?
非常感谢。
回答
起初,我尝试使用 IValueConverter 使用反射查找 HasValue 属性并调用它,不幸的是传递的 value 参数仍然是一个普通的旧 int。
我现在有以下内容:
public class HasValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value != null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
和 XAML:
<sdk:DataGridCheckBoxColumn Header="Invoiced" Binding="{Binding SalesInvoiceId, Converter={StaticResource HasValueConverter}}" />
I am having trouble with this binding:
<sdk:DataGridCheckBoxColumn Header="Invoiced" Binding="{Binding SalesInvoiceId.HasValue}" />
The error is:
System.Windows.Data Error: BindingExpression path error: 'HasValue' property not found on '6' 'System.Int32' (HashCode=6). BindingExpression: Path='SalesInvoiceId.HasValue' DataItem='Entities.DeliveryNote' (HashCode=5034835); target element is 'System.Windows.Controls.CheckBox' (Name=''); target property is 'IsChecked' (type 'System.Nullable`1[System.Boolean]')..
The field SalesInvoiceId is on the data context for the row, all other columns bind fine.
The type of SalesInvoiceId is int?
. And when looking at the object whilst debugging, it is shown as a int?
so the DataGrid should not have a problem with this - yet it is refering to it as if it is just an int
!
Is there anything I am doing wrong?
Many Thanks.
Answer
At first I tried an IValueConverter to look for the HasValue property using reflection and invoke it, unfortunatly the value parameter passed is STILL a plain old int.
I now have the following:
public class HasValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value != null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
And XAML:
<sdk:DataGridCheckBoxColumn Header="Invoiced" Binding="{Binding SalesInvoiceId, Converter={StaticResource HasValueConverter}}" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您是否可以在与 Silverlight 的绑定中使用 HasValue 表达式,您可能必须使用转换器或公开一个属性来为您执行此评估并返回 bool。
I'm not sure if you can have that HasValue expression in the binding with Silverlight, you might have to use a converter or expose a property doing this evaluation for you and returning a bool.