【发布时间】:2013-06-24 16:33:45
【问题描述】:
我正在尝试将一组可为空值 (Items=new ObservableCollection<double?>{}) 绑定到数据网格。下面给了我错误
值不能为空。 参数名称:key
<DataGrid Name="pointList" ItemsSource="{Binding Path=Value.Items,Converter={l:SelectableListArrayToListConverter}}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Value" Binding="{Binding}"/>
</DataGrid.Columns>
</DataGrid>
当我尝试使用转换器时,我收到以下错误 双向绑定需要 Path 或 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;
}
}
我认为上述错误是因为双向绑定不适用于 List list = new List();
我相信当 itemssource 在设置 Itemssource 之后但在设置 DataGridTextColumn Binding 之前构建行时,我会遇到错误。
到目前为止,我已经广泛尝试找到解决此问题的方法,但运气不佳。
如果这篇文章有任何问题,请告诉我,我会纠正它。
谢谢。
【问题讨论】:
-
请告诉我们哪一行抛出了这个异常。
-
为什么key是空的?
-
它没有给我一行,只是说 InvalidOperationException is unhandled {"Two-way binding requires Path or XPath."}
-
我不确定为什么密钥为空。我提供了一个可观察的集合(它有一个键)。 ItemsSource 必须根据我的列表创建一个键,因为我的列表可以为空并且有一个 null 它这是 null 必须来自的地方
标签: c# wpf data-binding datagrid nullable