繁体   English   中英

绑定到 WPF DataGrid 时,此视图不允许 DataGrid 版本“EditItem”

[英]DataGrid edition 'EditItem' is not allowed for this view` when bound to a WPF DataGrid

我至少已经阅读了 4 个小时,并且似乎是列表类型,但我有一个情况:

具有集合属性的 ObservableCollection。

我定义了第一个 DataGrid,并在部分

<DataGrid.RowDetailsTemplate>
    <DataTemplate>
        <!-- second Datagrid here, binding to Level2 property of my Observable collection -->
    </DataTemplate>
<DataGrid.RowDetailsTemplate>

一切都很好,屏幕上的所有东西都如我所料......但是:

  1. 如果尝试修改 DataGrid1 单元格,它允许我。
  2. 如果尝试修改 DataGrid2 单元格,则会向我抛出此异常'EditItem' is not allowed for this view

我错过了什么?

这是我的模型:

public partial class Level1
{
    public Level1()
    {
        this.Level2 = new HashSet<Level2>();
    }

    public decimal IdLevel1 { get; set; }
    public decimal IdLevel2 { get; set; }
    public string StrDescripcionTipoAsociado {get;set;}

    public virtual Level2 Level2{ get; set; }
}

public partial class Level2
{
    public decimal IdLevel1 { get; set; }
    public decimal IdLevel3 { get; set; }

    public virtual Level3 Level3{ get; set; }
}

public partial class Level3
{
    public decimal IdLevel3 { get; set; }
    public decimal NumIdConcepto {get;set;}
    public string StrDescripcionConcepto {get;set;}
}

编辑:XAML 代码:

    <DataGrid Grid.Row="1" 
              ItemsSource="{Binding Level1}" 
              AutoGenerateColumns="False" 
              SelectionMode="Single"
              GridLinesVisibility="Vertical"
              CanUserAddRows="True"
              CanUserDeleteRows="True"
              x:Name="GridTipoAsociado">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Tipo de asociado" x:Name="TipoUsuarioSeleccionado">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Label Style="{StaticResource ResourceKey=FontElemNivel1}" Content="{Binding StrDescripcionTipoAsociado}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <TextBox Style="{StaticResource ResourceKey=FontElemNivel2}" Text="{Binding StrDescripcionTipoAsociado }"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <DataGrid Grid.Row="1" 
                          ItemsSource="{Binding Level2}" 
                          AutoGenerateColumns="False" 
                          SelectionMode="Single"
                          SelectionUnit="Cell"
                          GridLinesVisibility="Vertical"
                          CanUserAddRows="True"
                          CanUserDeleteRows="True"                            
                          x:Name="GridItems">
                    <DataGrid.Columns>
                        <DataGridTemplateColumn Header="Id Item">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Label Content="{Binding NumIdConcepto}"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTemplateColumn Header="Items">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Label Content="{Binding Level3.StrDescripcionConcepto}"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                            <DataGridTemplateColumn.CellEditingTemplate>
                                <DataTemplate>
                                    <TextBox Text="{Binding Level3.StrDescripcionConcepto}"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellEditingTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
    </DataGrid>

我试过了,问题是你已经将 Level2 集合初始化为Hashset<> IEditableCollectionView.EditItem()在尝试更新Hashset<>项目时抛出此错误。 我将集合初始化为List<>并且它工作正常。

我不确定为什么它无法更新哈希集中的项目,需要更深入地研究它。 但是将Hashset<>更改为List<>将修复此错误。

希望能帮助到你

谢谢

你可以试试这个。 将一个 BeginningEdit 处理程序附加到您的 DataGrid 并指向以下代码:

    private void Grid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
    {
        //// Have to do this in the unusual case where the border of the cell gets selected.
        //// and causes a crash 'EditItem is not allowed'
        e.Cancel = true;
    }

只有当您以某种方式设法物理点击单元格的边界时,这才会命中。 事件的 OriginalSource 是一个边框,我认为这里可能发生的情况是,不是 TextBox 或其他可编辑元素作为预期的源,而是通过此不可编辑的边框进行编辑,这会导致隐藏在 ' EditItem 是不允许的例外。 在它可以通过其无效的 OriginalSource 冒泡之前取消此 RoutedEvent 可以阻止该错误发生在其轨道中。

向@nit 致谢,他们给了我正确的道路。 当然,问题在于 EF 集合的基本类型

Hashet< T >和 Datagrid 至少需要一个List< T > ,改变我所有的类“那些由实体框架生成的”,给我另一个问题,必须手动进行更改,我有很多。

我的解决方案是创建一个转换器,这对我来说是肮脏的工作:

public class listToObservableCollection : BaseConverter, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        HashSet<Level2> observableList = (HashSet<Level2>)value;
        return new ObservableCollection<Level2>(observableList);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (HashSet<Level2>)value;
    }
}

public abstract class BaseConverter : MarkupExtension
{
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

并将其放在我的 Datagrid2 的绑定上:

<!--part of my window definition--!>
xmlns:l="clr-namespace:Recursos;assembly=Recursos"
...
<!--part of my resources section--!>
<l:listToObservableCollection x:Key="listoToObservable"/>
...
<!--part of my datagrid definition--!>
ItemsSource="{Binding Level2,Converter={StaticResource listoToObservable}}"

唯一在播的是如何制作通用转换器,但现在它工作正常。

我还通过使用IsReadOnly="True"解决了这个问题。

这是我使用的通用转换器

public class ObservableCollectionConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var observableType= typeof (ObservableCollection<>).MakeGenericType(value.GetType().GetGenericArguments());
        return Activator.CreateInstance(observableType, value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var observableType = typeof(HashSet<>).MakeGenericType(value.GetType().GetGenericArguments());
        return Activator.CreateInstance(observableType, value);
    }
}

用更短的方式你可以写:

DataGrid.BeginningEdit += (s, ss) => ss.Cancel = true;

您可以设置 IsReadOnly 属性。 也许异常不会发生......在xaml文件中尝试......

IsReadOnly="True"

看到没有人发布这个,答案是因为您绑定到的集合需要实现IList (可能还有ICollection )。

这是由于DataGrid的非泛型(想想object )性质(我相信也是CollectionView )。

我解决了这个问题,将我的数据网格置于只读模式

<DataGrid 
                Name="dtgBulkInsert"
                CanUserSortColumns="True" 
                Height="300" Visibility="Collapsed" IsReadOnly="True">

....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM