【问题标题】:Binding a collection of nullable values to a DataGrid将可为空值的集合绑定到 DataGrid
【发布时间】: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


【解决方案1】:

我找到了以下链接link,它说我需要对列表中的项目使用包装器。我在我的对象中创建了第二个属性,它将我的原始列表转换为一个包装列表并绑定到它。我现在要做的就是监视绑定列表中的任何更改并相应地更新我的原始列表。感谢您的帮助。

【讨论】:

    【解决方案2】:

    我认为您的绑定不正确。检查 Value.Items 的绑定。

    试试这个。

     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; }
    

    XAML:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 2015-03-07
      • 2011-01-02
      • 1970-01-01
      相关资源
      最近更新 更多