不允许在DataGrid绑定到Double类型的列表或可观察集合时进行添加行操作。

huangapple go评论103阅读模式
英文:

No Add Rows Operation allowed when a DataGrid binds to a list or observable collection of Double

问题

我有一个在我的ViewModel中定义的ObservableCollection<double>

ListWidthsFlat=new ObservableCollection<double>();
ListWidthsFlat.Add(120);
ListWidthsFlat.Add(200);

我的XAML代码:

<DataGrid ItemsSource="{Binding Path=ListWidthsFlat}" AutoGenerateColumns="False"
 CanUserAddRows="True" CanUserDeleteRows="True" HorizontalAlignment="Center">
    <DataGrid.Columns>
        <DataGridTextColumn Header="{x:Static p:Resources.Width}" Binding="{Binding ., UpdateSourceTrigger=PropertyChanged}" Width="100" />
    </DataGrid.Columns>
</DataGrid>

我想要的是显示ObservableCollection,然后提供添加/删除项到我的ObservableCollection<double>的可能性。

当我对ObservableCollection<T>做同样的事情时,一切都正常工作。但是当绑定到ObservableCollection<double>时,似乎参数CanUserAddRows不起作用。

编辑

经过额外的测试,似乎问题是当我将DataGrid绑定到ObservableCollection<T>时,设置CanUserAddRow=True,会自动创建一个额外的空行(这样我可以编辑它并向ObservableCollection添加新项)。

当我将DataGrid绑定到ObservableCollection<double>时,不会创建空行。

这是一个截图,以便更容易理解:

不允许在DataGrid绑定到Double类型的列表或可观察集合时进行添加行操作。

英文:

I have an ObservableCollection<double> that is defined in my ViewModel.

ListWidthsFlat=new ObservableCollection<double>();
ListWidthsFlat.Add(120);
ListWidthsFlat.Add(200);

My XAML code :

<DataGrid ItemsSource="{Binding Path=ListWidthsFlat}" AutoGenerateColumns="False"
 CanUserAddRows="True" CanUserDeleteRows="True" HorizontalAlignment="Center">
    <DataGrid.Columns>
        <DataGridTextColumn Header="{x:Static p:Resources.Width}" Binding="{Binding ., UpdateSourceTrigger=PropertyChanged}" Width="100" />
    </DataGrid.Columns>
</DataGrid>

What I want is show the ObservableCollection, then offer possibility to add/delete items from my ObservableCollection<double>.

When I do the same thing on an ObservableCollection<T> all is working perfectly.
But when binding to ObservableCollection<double>, seems that parameters CanUserAddRows is not working.

Edit :

After additional tests, it seems the problem is that when I bind a DataGrid to an ObservableCollection<T>, and set CanUserAddRow=True, an additional empty line is automatically created (so I can edit it and add a new item to ObservableCollection.

When I bind a DataGrid to ObservableCollection<double>, no empty line is created.

Here is a screenshot to make it more understandable :

不允许在DataGrid绑定到Double类型的列表或可观察集合时进行添加行操作。

答案1

得分: 2

为了正确执行CanUserAddRows,绑定到的对象列表必须实现IEditableCollectionView Interface,该接口为绑定的集合提供了基本的编辑功能。在其中,从列表中呈现的项目必须具有公共的无参数构造函数。

由于值类型double没有构造函数,因此网格会检测到这一点,并且不提供添加行;因此,你仅在double上看到失败,但它适用于对象()的实例,这些实例具有List<T>作为特定属性。


为了解决这个限制,

  1. 创建一个具有公共无参数构造函数和一个double属性的类。
  2. 然后创建您的类列表,并将其绑定到ObservableCollection(或者如果您不需要observablecollection的开销,也可以使用List),并设置一组您的值。
  3. 在Xaml中,将数据网格中的列指向double属性。
  4. 您可能需要编写一个值构造函数,该构造函数将接受一个浮点数并返回一个字符串,并将字符串转换为浮点数。
英文:

To properly do a CanUserAddRows the object list being bound to must implement IEditableCollectionView Interface which provide basic editing capabilities to the collection being bound to. Within that the item being presented from the list has to have a public default parameterless constructor.

Because a value type double does not have a constructor the grid detects that and does not provide an add row; hence you see the failure on double alone, but it works on the object, (class) instances of List<T> which have double as a specific property.


To work around the limitation,

  1. Create a class which has a public parameterless constructor and one double property.
  2. Then create your list of class and bind to that ObservableCollection (or List works too actually if you don't need the overhead of the observablecollection.) with a set of your values.
  3. In Xaml set the column in the datagrid to point to the double property.
  4. You may need to write a value constructor which will take in a float and return a string, and convert a string to a float.

huangapple
  • 本文由 发表于 2020年1月4日 01:27:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定