Reputation: 83
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.
Upvotes: 0
Views: 1230
Reputation: 5691
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>
Upvotes: 0
Reputation: 83
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.
Upvotes: 0