问题描述
我已经阅读了至少 4 个小时,似乎是列表类型,但我有一个情况:
I've been reading about this at least for 4 hours, and seems to be the list type, but I have a situation:
具有集合属性的 ObservableCollection.
A ObservableCollection that has a collection property.
我定义了第一个DataGrid,并在部分
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:
- 如果尝试修改 DataGrid1 单元格,它允许我.
- 如果尝试修改 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>
推荐答案
感谢@nit 给我正确的道路.当然问题在于EF集合的基本类型
Tks to @nit who give me the right path. Of course the problem reside on the base Type of EF collections
哈希 和Datagrid 至少需要一个List,把我所有的类都改成那些由实体框架生成的",又给我带来了一个问题,必须手动修改,我有很多.
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;
}
}
并将其放在我的 Datagrid2 的绑定上:
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.
这篇关于绑定到 WPF DataGrid 时,此视图不允许 DataGrid 版本“EditItem"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!