此视图不允许使用“EditItem”。 WPF .NET 4 / EF 4.1 - 主 - 详细 [英] 'EditItem' is not allowed for this view. WPF .NET 4 / EF 4.1 - Master - Detail
问题描述
好的,为了尝试找到这个问题的答案,我使用了AdventureWorks数据库,使一个更简单的项目。
Ok, in order to try to find an answer for this question I have made an even simpler project, using the AdventureWorks database.
我创建了一个模型从供应商和PurchaseOrderHeader表 - 将它们作为数据源添加到项目中,并具有以下代码:
I created a Model from the Vendor and PurchaseOrderHeader tables - added them as a datasource to the project, and have the following code:
XAML:
<Window x:Class="EFDbContextTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="569" Width="1130" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:EFDbContextTest;assembly=EFDbContextTest" Loaded="Window_Loaded">
<Window.Resources>
<CollectionViewSource x:Key="vendorViewSource" d:DesignSource="{d:DesignInstance my:Vendor, CreateList=True}" />
<CollectionViewSource x:Key="vendorPurchaseOrderHeadersViewSource" Source="{Binding Path=PurchaseOrderHeaders, Source={StaticResource vendorViewSource}}" />
</Window.Resources>
<Grid DataContext="{StaticResource vendorViewSource}">
<DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="200" HorizontalAlignment="Left" ItemsSource="{Binding}" Margin="12,12,0,0"
Name="vendorDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="1084" IsSynchronizedWithCurrentItem="True"
SelectedValuePath="VendorID">
<DataGrid.Columns>
<DataGridTextColumn x:Name="accountNumberColumn" Binding="{Binding Path=AccountNumber}" Header="Account Number" Width="SizeToHeader" />
<DataGridCheckBoxColumn x:Name="activeFlagColumn" Binding="{Binding Path=ActiveFlag}" Header="Active Flag" Width="SizeToHeader" />
<DataGridTextColumn x:Name="creditRatingColumn" Binding="{Binding Path=CreditRating}" Header="Credit Rating" Width="SizeToHeader" />
<DataGridTemplateColumn x:Name="modifiedDateColumn" Header="Modified Date" Width="SizeToHeader">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Path=ModifiedDate, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn x:Name="nameColumn" Binding="{Binding Path=Name}" Header="Name" Width="SizeToHeader" />
<DataGridCheckBoxColumn x:Name="preferredVendorStatusColumn" Binding="{Binding Path=PreferredVendorStatus}" Header="Preferred Vendor Status" Width="SizeToHeader" />
<DataGridTextColumn x:Name="purchasingWebServiceURLColumn" Binding="{Binding Path=PurchasingWebServiceURL}" Header="Purchasing Web Service URL" Width="SizeToHeader" />
<DataGridTextColumn x:Name="vendorIDColumn" Binding="{Binding Path=VendorID}" Header="Vendor ID" Width="SizeToHeader" />
</DataGrid.Columns>
</DataGrid>
<DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="200" HorizontalAlignment="Left"
ItemsSource="{Binding Source={StaticResource vendorPurchaseOrderHeadersViewSource}}" Margin="12,218,0,0" Name="purchaseOrderHeadersDataGrid"
RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="1084" IsSynchronizedWithCurrentItem="True">
<DataGrid.Columns>
<DataGridTextColumn x:Name="employeeIDColumn" Binding="{Binding Path=EmployeeID}" Header="Employee ID" Width="SizeToHeader" />
<DataGridTextColumn x:Name="freightColumn" Binding="{Binding Path=Freight}" Header="Freight" Width="SizeToHeader" />
<DataGridTemplateColumn x:Name="modifiedDateColumn1" Header="Modified Date" Width="SizeToHeader">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Path=ModifiedDate, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn x:Name="orderDateColumn" Header="Order Date" Width="SizeToHeader">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Path=OrderDate, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn x:Name="purchaseOrderIDColumn" Binding="{Binding Path=PurchaseOrderID}" Header="Purchase Order ID" Width="SizeToHeader" />
<DataGridTextColumn x:Name="revisionNumberColumn" Binding="{Binding Path=RevisionNumber}" Header="Revision Number" Width="SizeToHeader" />
<DataGridTemplateColumn x:Name="shipDateColumn" Header="Ship Date" Width="SizeToHeader">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Path=ShipDate, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn x:Name="shipMethodIDColumn" Binding="{Binding Path=ShipMethodID}" Header="Ship Method ID" Width="SizeToHeader" />
<DataGridTextColumn x:Name="statusColumn" Binding="{Binding Path=Status}" Header="Status" Width="SizeToHeader" />
<DataGridTextColumn x:Name="subTotalColumn" Binding="{Binding Path=SubTotal}" Header="Sub Total" Width="SizeToHeader" />
<DataGridTextColumn x:Name="taxAmtColumn" Binding="{Binding Path=TaxAmt}" Header="Tax Amt" Width="SizeToHeader" />
<DataGridTextColumn x:Name="totalDueColumn" Binding="{Binding Path=TotalDue}" Header="Total Due" Width="SizeToHeader" />
<DataGridTextColumn x:Name="vendorIDColumn1" Binding="{Binding Path=VendorID}" Header="Vendor ID" Width="SizeToHeader" />
</DataGrid.Columns>
</DataGrid>
</Grid>
C#:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace EFDbContextTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Entities _context = new Entities();
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
System.Windows.Data.CollectionViewSource vendorViewSource =
((System.Windows.Data.CollectionViewSource)(this.FindResource("vendorViewSource")));
_context.Vendors.Include("PurchaseOrderHeaders").Load();
vendorViewSource.Source = _context.Vendors.Local;
}
}
}
两个网格都在窗口上,您可以编辑/删除/更新主网格,就像没有明天一样 - 只要您尝试单击细节网格上的任何单元格,就会看到该视图不允许出现EditItem错误。
So, both grids are on the Window, you can edit/delete/update the Master grid like there's no tomorrow - as soon as you try to click into any cell on the detail grid you get the error 'EditItem' is not allowed for this view.
我必须在这里缺少一些东西,因为这在几个例子中发生,我使用EF 4.1 - 帮助! b $ b
I have to be missing something here, because this happens to me in several instances where I have a Master-Detail view using EF 4.1 - help!
推荐答案
如果您的供应商$中的
PurchaseOrderHeaders
c $ c>类是 ICollection< T>
类型,那么这可能是你的问题的根源。要获得允许编辑源集合的数据网格,必须是类型 IList
IList
like List< T>
或 ObservableCollection< T>
。您可能需要相应地更改您的模型类中 PurchaseOrderHeaders
的收集类型。
If the PurchaseOrderHeaders
collection property in your Vendor
class is of type ICollection<T>
then this could be source of your problem. To get a datagrid which allows editing the source collection must be of type IList<T>
or any type which implements IList<T>
like List<T>
or ObservableCollection<T>
. You will probably need to change the collection type of PurchaseOrderHeaders
in your model class accordingly.
更详细的解释在这里:使用EF POCO对象获取只读数据绑定
More detailed explanation is here: Getting Readonly Databind using EF POCO Objects
编辑
根据@Allon Guralnek的评论,有必要实现非泛型 IList
接口来获取可编辑的DataGrid。对于 ObservableCollection< T>
和 List< T>
的情况。仅实现通用 IList< T>
但不是非通用 IList
不会使DataGrid可编辑。
According to @Allon Guralnek's comment below it is necessary to implement the non-generic IList
interface to get an editable DataGrid. This is the case for ObservableCollection<T>
and List<T>
. Other implementations which only implement the generic IList<T>
but not the non-generic IList
won't make the DataGrid editable.
这篇关于此视图不允许使用“EditItem”。 WPF .NET 4 / EF 4.1 - 主 - 详细的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!