开发者

Binding to HasValue on Nullable type with Silverlight DataGrid

开发者 https://www.devze.com 2023-03-02 14:43 出处:网络
I am having trouble with this binding: <sdk:DataGridCheckBoxColumn Header=\"Invoiced\" Binding=\"{Binding SalesI开发者_C百科nvoiceId.HasValue}\" />

I am having trouble with this binding:

<sdk:DataGridCheckBoxColumn Header="Invoiced" Binding="{Binding SalesI开发者_C百科nvoiceId.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}}" />


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消