为什么我的一个WPF DataGrids中的一个授予“EditItem”不允许这个视图“例外? [英] Why does one of MY WPF DataGrids give the "'EditItem' is not allowed for this view" exception?

查看:1928
本文介绍了为什么我的一个WPF DataGrids中的一个授予“EditItem”不允许这个视图“例外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了所有可以在这里和关于这个例外的MS论坛上找到的问答,并尝试了我理解的大部分建议,还有其他几个。似乎这个异常可能会出现各种各样的原因。



与其他人一样,我将WPF DataGrid绑定到一个集合,当一个尝试编辑其中一个单元格。他们被设置为可写,集合是一个ObservableCollection,我已经实现了get和set处理程序,发送通知消息。



我没有尝试的建议是执行IList非通用接口的那些,因为我不知道该怎么做。此外,我有很多DataGrids绑定到我的应用程序中的各种列表和集合,这个工作,当它绑定到一个LINQ集合时,这个用于工作的。



请帮助我想知道我需要做什么。



数据网格是:

 < DataGrid Name =dgIngredientsMargin =567,32,0,44Width =360ItemsSource ={Binding}IsReadOnly =False
AutoGenerateColumns =FalseHorizo​​ntalAlignment =LeftCanUserAddRows =FalseCanUserDeleteRows =False>
< DataGrid.Columns>
< DataGridTextColumn Width =63Header =PercentBinding ={Binding Preference}IsReadOnly =False/>
< DataGridTextColumn SortDirection =DescendingWidth =301Header =IngredientBinding ={Binding Ingredient}IsReadOnly =TrueCanUserSort =TrueCanUserReorder =False/>
< /DataGrid.Columns>
< / DataGrid>

正在编辑的列是不可读的列,首选项。



集合是:

  private ObservableCollection< RAM_Ingredient> MemberIngredientPrefs = new ObservableCollection< RAM_Ingredient>(); 

绑定是:

  dgIngredients.DataContext = MemberIngredientPrefs.OrderBy(Ingredient,true); 

RAM_Ingredient是:

  public class RAM_Ingredient:INotifyPropertyChanged 

等。



其中RAM_Ingredient.Preference是:

  private int _Preference; 
public int首选项
{
get
{
return _Preference;
}
set
{
//这是需要发送更改通知(并且不会在网格编辑中抛出异常!):
if((_Preference != value))
{
SendPropertyChanging();
_Preference = value;
SendPropertyChanged(偏好);
}
}
}

异常是: p>

  System.InvalidOperationException未处理
此视图不允许Message ='EditItem'。
Source = PresentationFramework
StackTrace:
在System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem(对象项)
在System.Windows.Controls.DataGrid。 EditRowItem(Object rowItem)
在System.Windows.Controls.DataGrid.OnExecutedBeginEdit(ExecutedRoutedEventArgs e)

etc ...

解决方案

我也有这个问题,发现这里的一点是我们无法编辑在DataGrid中的IEnumerable中,只能列出一个列表。



因此,我们不需要创建一个新类,它也在具有匿名返回类型的LINQ查询上工作。它只需要一个列表。



这是我的代码示例:

  dtgPrdcts.ItemsSource = ProductLists.Select(Function(x)New With {.ListTitle = x.ListTitle,.ProductID = x.ProductID,.License =,.ForRemove = True})ToList 


I have read all the Q&A I could find here and on the MS forums about this exception, and tried most of the suggestions that I understood, and a few others. It seems that this exception can come up for a wide range of causes.

As with others, I have a WPF DataGrid bound to a collection, which throws this exception when one tries to edit one of the cells. They are set to be write-able, the collection is an ObservableCollection, I've implemented get and set handlers which send notification messages.

The suggestions I haven't tried are the ones involving implementing IList's non-generic interface, because I have no idea what I would do to do that. Also, I have many DataGrids bound to various lists and collections in my app which work, and this one used to work when it was bound to a LINQ collection.

Please help me figure out what I need to do here.

The Data Grid is:

<DataGrid Name="dgIngredients" Margin="567,32,0,44" Width="360" ItemsSource="{Binding}" IsReadOnly="False"
           AutoGenerateColumns="False" HorizontalAlignment="Left" CanUserAddRows="False" CanUserDeleteRows="False">
    <DataGrid.Columns>
        <DataGridTextColumn Width="63" Header="Percent" Binding="{Binding Preference}" IsReadOnly="False" />
        <DataGridTextColumn SortDirection="Descending" Width="301" Header="Ingredient" Binding="{Binding Ingredient}" IsReadOnly="True" CanUserSort="True" CanUserReorder="False" />
    </DataGrid.Columns>
</DataGrid>

The column being edited is the non-read-only one, Preference.

The collection is:

private ObservableCollection<RAM_Ingredient> MemberIngredientPrefs = new ObservableCollection<RAM_Ingredient>();

The binding is:

dgIngredients.DataContext = MemberIngredientPrefs.OrderBy("Ingredient",true);

RAM_Ingredient is:

public class RAM_Ingredient : INotifyPropertyChanged 

etc.

Where RAM_Ingredient.Preference is:

private int _Preference;
public int Preference
{
    get
    {
        return _Preference;
    }
    set
    {
        // This is needed to send notification of changes (and to not throw an exception on grid edit!):
        if ((_Preference != value))
        {
            SendPropertyChanging();
            _Preference = value;
            SendPropertyChanged("Preference");
        }
    }
}

The exception is:

System.InvalidOperationException was unhandled
  Message='EditItem' is not allowed for this view.
  Source=PresentationFramework
  StackTrace:
       at System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem(Object item)
       at System.Windows.Controls.DataGrid.EditRowItem(Object rowItem)
       at System.Windows.Controls.DataGrid.OnExecutedBeginEdit(ExecutedRoutedEventArgs e)

etc...

解决方案

I have also this problem, And found that the point here is that we can not edit a IEnumerable in a DataGrid, only a list can be edited.

therefore we didn't need to create a new class, its works also on a LINQ query with anonymous return type. it's need only to be a list.

here is a sample of my code:

 dtgPrdcts.ItemsSource= ProductLists.Select(Function(x) New With {.ListTitle = x.ListTitle, .ProductID = x.ProductID, .License = "", .ForRemove = True}).ToList

这篇关于为什么我的一个WPF DataGrids中的一个授予“EditItem”不允许这个视图“例外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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