0
votes

I am attempting to bind a collection of nullable values (Items=new ObservableCollection<double?>{}) to a datagrid. The below gives me the error

Value cannot be null. Parameter name: key

            <DataGrid Name="pointList" ItemsSource="{Binding Path=Value.Items,Converter={l:SelectableListArrayToListConverter}}" AutoGenerateColumns="False" >
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Value" Binding="{Binding}"/>
                </DataGrid.Columns>
            </DataGrid>

when i try to use a converter i get the following error Two-way binding requires Path or XPath.

            <DataGrid Name="pointList" ItemsSource="{Binding Path=Value.Items,Converter={l:SelectableListArrayToListConverter}}" AutoGenerateColumns="False" >
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Value" Binding="{Binding}"/>
                </DataGrid.Columns>
            </DataGrid>

public class SelectableListArrayToListConverter : MarkupExtension, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is IEnumerable)
        { 
            List<string> list = new List<string>();
            foreach(var item in value as IEnumerable )
            {
                if (item == null)
                    list.Add("NON");
                else
                    list.Add(item.ToString());

            }
            //Two-way binding requires Path or XPath

            return list;
        }

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

I believe that the above error is because twoway binding isnt working with List list = new List();

I believe i am getting the error when itemssource builds the rows after Itemssource is set but before DataGridTextColumn Binding is set.

I have tried extensively to find a solution to this problem with not luck so far.

If there is any issue with this post please let me know and i will correct it.

Thanks.

2
Please tell us which line throws this exception.Daniel Hilgarth
Why is the key null ?Paul Zahra
It doesnt give me a line just says InvalidOperationException was unhandled {"Two-way binding requires Path or XPath."}viciouskinid
I am not sure why the key is null. I am providing an observable collection (it dosent have a key). ItemsSource must be creating a key based on my list, because my list is nullable and has a null it it this is where the null must be coming fromviciouskinid

2 Answers

0
votes

I have found the following link link that says i need to use a wrapper for the items in my list. I created a second property in my object that convert my original list to a wrapped list and bind to that. All i have to do now is monitor any changes in the bound list and update my original list accordingly . Thanks for the help.

0
votes

I think your binding is not correct. check the binding for Value.Items.

try this.

 public Window2()
    {
        InitializeComponent();
        if (Items == null)
            Items = new ObservableCollection<double?>();

        for (int i = 0; i < 50; i++)
        {
            if (i % 5 == 0)
                Items.Add(null);
            else
                Items.Add(i);
        }

        this.DataContext = this;
    }

    public ObservableCollection<double?> Items { get; set; }

The XAML:

<DataGrid Name="pointList" ItemsSource="{Binding Path=Items,Converter={local:SelectableListArrayToListConverter}}" AutoGenerateColumns="False" >
                            <DataGrid.Columns>
                                <DataGridTextColumn Header="Value" Binding="{Binding}"/>
                            </DataGrid.Columns>
                        </DataGrid>