此视图列表不允许使用Edititem [英] Edititem is not allowed for this view list

查看:95
本文介绍了此视图列表不允许使用Edititem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了好几个小时,但是我无法在datagrid中编辑列数量,每当执行此操作时,都会出现错误,提示我

I have tried for hours and hours but I cannot edit the column quantity in datagrid, whenever I do it gives me an error saying that


PresentationFramework.dll
中发生了类型为'System.InvalidOperationException'的未处理异常。其他信息:此视图不允许使用'EditItem'。

An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll Additional information: 'EditItem' is not allowed for this view.

我的xaml代码是

<DataGrid EnableRowVirtualization="True" Grid.Row="3" Grid.ColumnSpan="2" AutoGenerateColumns="False" Name="DataGrid1" IsReadOnly="False" ItemsSource="{Binding Products}" Margin="10,10,10,10" PreviewKeyDown="DataGrid1_PreviewKeyDown" SelectionChanged="DataGrid1_SelectionChanged" CellEditEnding="DataGrid1_CellEditEnding" CanUserAddRows="True" CanUserDeleteRows="True" BeginningEdit="DataGrid1_BeginningEdit" >
    <DataGrid.Columns>
        <DataGridTextColumn Header="Item Name" IsReadOnly="True" Binding="{Binding Path=ItemName}" Width="*"></DataGridTextColumn>
            <DataGridTextColumn Header="Item Price" IsReadOnly="True"  Binding="{Binding Path=ItemPrice}" Width="*"></DataGridTextColumn>
            <DataGridTextColumn x:Name="QuantityColumn" Header="Quantity" IsReadOnly="False"  Binding="{Binding Path=Quantity, Mode=TwoWay}" Width="*"></DataGridTextColumn>
            <DataGridTextColumn Header="Total Price" IsReadOnly="True" Binding="{Binding Path=TotalPrice}" Width="*">
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

这是C#代码

List<AddItem> DATA = new List<AddItem>()
{
    new AddItem()
    {
        ItemName = ItemName.Text.ToString(),
        ItemPrice = float.Parse(ItemPrice.Text.ToString()),
        Quantity = quantity.Text,
        TotalPrice = CalculateTotalPrice()
    }
};
DataGrid1.Items.Add(DATA);

public class AddItem
{
    public string ItemName { get; set; }
    public float ItemPrice { get; set; }
    public string Price { get; set; }
    public string Quantity { get; set; }
    public decimal TotalPrice { get; set; }
}

我要去哪里了?我已经尝试了可观察的收集还还是没有解决办法?
任何帮助将不胜感激。

Where am I going wrong? I have tried observable collection also still no solution? Any Help will be appreciated.

推荐答案

分配 List< AddItem> 列表添加到 ItemSource 而不是 Add ,如下所示:

Assign the List<AddItem> list to the ItemSource instead of Add like so:

使用

DataGrid1.ItemsSource = DATA;

而不是

DataGrid1.Items.Add(DATA);

代码的其他改进:


  • 使用十进制类型表示所有价格(始终)

  • 使用 int 类型的数量,因为数量代表数字

  • Use decimal type for all prices (always)
  • Use int type for quantity since quantity represent a number

这篇关于此视图列表不允许使用Edititem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆