为了试图找到这个问题的答案,我做了一个更简单的项目,使用AdventureWorks数据库。
我从Vendor和PurchaseOrderHeader表中创建了一个模型 - 将它们作为数据源添加到项目中,并有以下代码:
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操作”。
我必须错过了什么,因为在使用EF 4.1的多个实例中,我都遇到了这种情况 - 求助!
IList<T>
不行,关键在于让你的集合实现非泛型的IList
接口。 - Allon GuralnekObservableCollection<T>
。谢谢您的澄清,我没想到非泛型的IList
是重要的部分。 - Slauma