数据网格版“EditItem'不允许这种view`时绑定到一个DataGrid的WPF [英] DataGrid edition 'EditItem' is not allowed for this view` when bound to a WPF DataGrid

查看:540
本文介绍了数据网格版“EditItem'不允许这种view`时绑定到一个DataGrid的WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读有关这至少4小时,似乎是列表类型,但我有一个情况:



一个的ObservableCollection,有一个集合属性。



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



<预类=郎咸平的XML prettyprint-覆盖> < DataGrid.RowDetailsTemplate>
<&DataTemplate的GT;
<! - 第二个DataGrid这里,结合我观察到的集合的财产级别2 - >
< / DataTemplate中>
< DataGrid.RowDetailsTemplate>



一切顺利,在屏幕上所有的事情如我所料...但是:




  1. 如果试图修改DataGrid1中细胞中,这让我。

  2. 如果试图修改DataGrid2细胞中,这把我这个异常'EditItem'不允许这种看法



我在想什么?



这是我的模型:



<预类=郎-CS prettyprint-覆盖> 公共部分类1级
{
公共1级()
{
this.Level2 =新的HashSet<&2级别GT;();
}

公共小数IdLevel1 {搞定;组; }
公共小数IdLevel2 {搞定;组; }
公共字符串StrDescripcionTipoAsociado {获取;设置;}

公用虚拟级别2级别2 {搞定;组; }
}

公共部分类级别2
{
公共小数IdLevel1 {搞定;组; }
公共小数IdLevel3 {搞定;组; }

公共虚拟Level3的Level3的{搞定;组; }
}

公共部分类Level3的
{
公共小数IdLevel3 {搞定;组; }
公共小数NumIdConcepto {获取;设置;}
公共字符串StrDescripcionConcepto {获取;设置;}
}

编辑:XAML代码:



<预类=郎咸平的XML prettyprint-覆盖> <数据网格Grid.Row =1
的ItemsSource ={结合级别1}
的AutoGenerateColumns =假
的SelectionMode =单
GridLinesVisibility =垂直
CanUserAddRows =真
CanUserDeleteRows =真
X:NAME =GridTipoAsociado>
< DataGrid.Columns>
< DataGridTemplateColumn标题=TIPO德asociadoX:NAME =TipoUsuarioSeleccionado>
< DataGridTemplateColumn.CellTemplate>
<&DataTemplate的GT;
<标签样式={StaticResource的的ResourceKey = FontElemNivel1}CONTENT ={结合StrDescripcionTipoAsociado}/>
< / DataTemplate中>
< /DataGridTemplateColumn.CellTemplate>
< DataGridTemplateColumn.CellEditingTemplate>
<&DataTemplate的GT;
<文本框样式={StaticResource的的ResourceKey = FontElemNivel2}文本={结合StrDescripcionTipoAsociado}/>
< / DataTemplate中>
< /DataGridTemplateColumn.CellEditingTemplate>
< / DataGridTemplateColumn>
< /DataGrid.Columns>
< DataGrid.RowDetailsTemplate>
<&DataTemplate的GT;
<数据网格Grid.Row =1
的ItemsSource ={结合}级别2
的AutoGenerateColumns =FALSE
的SelectionMode =单
SelectionUnit = 细胞
GridLinesVisibility =垂直
CanUserAddRows =真
CanUserDeleteRows =真
X:NAME =GridItems>
< DataGrid.Columns>
< DataGridTemplateColumn标题=ID项目>
< DataGridTemplateColumn.CellTemplate>
<&DataTemplate的GT;
<标签内容={结合NumIdConcepto}/>
< / DataTemplate中>
< /DataGridTemplateColumn.CellTemplate>
< / DataGridTemplateColumn>
< DataGridTemplateColumn标题=项目>
< DataGridTemplateColumn.CellTemplate>
<&DataTemplate的GT;
<标签内容={结合Level3.StrDescripcionConcepto}/>
< / DataTemplate中>
< /DataGridTemplateColumn.CellTemplate>
< DataGridTemplateColumn.CellEditingTemplate>
<&DataTemplate的GT;
<文本框的文本={结合Level3.StrDescripcionConcepto}/>
< / DataTemplate中>
< /DataGridTemplateColumn.CellEditingTemplate>
< / DataGridTemplateColumn>
< /DataGrid.Columns>
< / DataGrid的>
< / DataTemplate中>
< /DataGrid.RowDetailsTemplate>
< / DataGrid的>


解决方案

韩国社交协会以@nit谁给我正确的道路。当然,问题驻留在EF集合



Hashet<的基本类型; T> 和DataGrid至少需要一个名单,LT; T> ,改变我的所有类的实体框架生成的,给我的另一个问题,必须手动更改,我有很多他们。



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

 公共类listToObservableCollection:BaseConverter,的IValueConverter 
{
公共对象转换(对象的值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
{
HashSet的<&2级别的GT; observableList =(HashSet的<&2级别GT;)的价值;
返回新的ObservableCollection<&2级别的GT;(observableList);
}

公共对象ConvertBack(对象的值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
{
回报率(HashSet的<级别2>)值;
}
}

公共抽象类BaseConverter:的MarkupExtension
{
公众覆盖对象ProvideValue(的IServiceProvider的ServiceProvider)
{
返回此;
}
}

和把它放在我Datagrid2的结合:

 <! - 我的窗口定义的一部分 - >!; 
的xmlns:L =CLR的命名空间:RECURSOS;装配= RECURSOS!

< - 我的资源部分的一部分 - >
< L:listToObservableCollection X:键=listoToObservable/>

<! - 我的DataGrid定义的一部分 - >!;
的ItemsSource ={结合级别2,转换器= {StaticResource的listoToObservable}}



唯一对空气的事情就是如何让一个通用的转换器,但现在它工作正常。


I've been reading about this at least for 4 hours, and seems to be the list type, but I have a situation:

A ObservableCollection that has a collection property.

I define the first DataGrid, and in the section

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

Everything goes fine, all things on screen as I expected... but:

  1. If try to modify DataGrid1 cells it allow me.
  2. If try to modify DataGrid2 cells it throw me this exception 'EditItem' is not allowed for this view

What am I missing ?

This is my model:

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

EDIT: XAML Code:

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

解决方案

Tks to @nit who give me the right path. Of course the problem reside on the base Type of EF collections

Hashet< T > And Datagrid need at least a List< T >, changing all my classes "those generated by Entity framework", give to me another problem, must make changes manually, and I have a lot of them.

My solution was to create a converter, that made the dirty work for me:

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

And put it on the binding of my 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}}"

The only thing on the air is how to make a generic converter, but for now it works fine.

这篇关于数据网格版“EditItem'不允许这种view`时绑定到一个DataGrid的WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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