If you have been working with WPF and have had issues with your ComboBox or any list control binding then you can stop pulling your hair out. The problem occurs when you try to bind the SelectedValue property of the control to a Nullable Int (int?). I am currently working with Microsoft to resolve the root problem, but here is a work around for now:
Create an IValueConveter:
public class NullIntToNegOneConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
{
return -1;
}
else
{
return value;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int i;
if (int.TryParse(value.ToString(), out i))
{
if (i == -1)
{
ufferoennePosted Jul 5, 2007, 5:02 AM
Hi, Great article but it would be nice if you could show how to reference it properly. I'm getting a Cannot find resource named 'NullIntToNegOneConverter' exception. Could you perhaps add that? /Uffe