【发布时间】:2017-04-26 14:30:33
【问题描述】:
上下文
使用 Entity Framework 6 我有一个机身对象的 ObservableCollection,每个对象都有一个下属的 Identity 对象集合。通过 XAML 和视图模型,我一次查看这个集合一个机身(主),它的每个身份(细节)对象都在 DataGrid 中。为了一次只处理一个 Airframe 的显示/更新,我使用 CollectionView 以便我在集合中获得当前位置,并可以连接“转到下一个”和“转到上一个”按钮和命令。一个简化的代码摘录:
背后的代码
private ADBContext databaseContext;
private UnitOfWork unitOfWork;
private ViewModels.ViewModel ViewModel;
public MainWindow()
{
InitializeComponent();
databaseContext = new ADBContext();
unitOfWork = new UnitOfWork(databaseContext);
ViewModel = new ViewModels.ViewModel(unitOfWork);
this.DataContext = ViewModel;
}
查看模型
public class ViewModel : INotifyPropertyChanged
{
public CollectionView AirframeCollectionView { get; set; }
public IUnitOfWork UnitOfWork;
public ViewModel(IUnitOfWork unitOfWork)
{
UnitOfWork = unitOfWork;
AirframeCollectionView = CollectionViewSource.GetDefaultView(new ObservableCollection<Airframe>(UnitOfWork.Airframes.GetAirframesForRegistration(SearchRegistration)));
RaisePropertyChanged("AirframeCollectionView");
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
XAML - 大师
<Grid.ColumnDefinitions>
<ColumnDefinition Name="airframeLabels" MaxWidth="100"/>
<ColumnDefinition Name="airframeDetails"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Name="typeRow"/>
<RowDefinition Name="constructionNoRow"/>
<RowDefinition Name="remarksRow"/>
<RowDefinition Name="rolledOutDateRow"/>
<RowDefinition Name="firstFlightDateRow"/>
<RowDefinition Name="statusRow"/>
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0">Type</Label>
<wpf:AutoCompleteTextBox Grid.Column="1" Grid.Row="0"
Text="{Binding Path=AirframeCollectionView/TypeName}"
Provider="{Binding TypeNameStubSuggestions}"/>
<Label Grid.Column="0" Grid.Row="1">Construction no</Label>
<TextBox Grid.Column="1" Grid.Row="1" Margin="5" Name="constructionNo"
Text="{Binding AirframeCollectionView/ConstructionNumber}"/>
<Label Grid.Column="0" Grid.Row="2">Remarks</Label>
<TextBox Grid.Column="1" Grid.Row="2" Margin="5" Name="remarks"
Text="{Binding AirframeCollectionView/Remarks}"/>
<Label Grid.Column="0" Grid.Row="3">Rolled out</Label>
<DatePickerTextBox Grid.Column="1" Margin="5" Grid.Row="3" Name="rolledOut"
Text="{Binding AirframeCollectionView/RolloutDate, StringFormat=\{0:dd-MMM-yy\}}"/>
<Label Grid.Column="0" Grid.Row="4">First flight</Label>
<DatePickerTextBox Grid.Column="1" Margin="5" Grid.Row="4" Name="firstFlight"
Text="{Binding AirframeCollectionView/FirstFlightDate, StringFormat=\{0:dd-MMM-yy\}}"/>
<Label Grid.Column="0" Grid.Row="5">Status</Label>
<ComboBox Grid.Column="1" Grid.Row="5" Margin="5" Name="status"
ItemsSource="{Binding AirframeStatuses}"
SelectedValue="{Binding AirframeCollectionView/StatusId, Mode=TwoWay}"
SelectedValuePath="StatusId"
DisplayMemberPath="StatusName"
SelectedItem="{Binding AirframeCollectionView/StatusId}"/>
XAML - 详细信息
<DataGrid Name="identitiesGrid"
AutoGenerateColumns="False"
ItemsSource="{Binding AirframeCollectionView/Identities, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsReadOnly="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Regn" Binding="{Binding Registration, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False"/>
</DataGrid>
问题
这完美地显示了数据,我可以毫无问题地编辑主数据。当我单击详细 DataGrid 中的任何行时,第一次单击选择单元格,第二次单击导致异常“不允许在 PresentationFramework.dll 中使用 'EditItem' 发生类型为 'System.InvalidOperationException' 的未处理异常”这个视图`”。
问题
如何停止这个异常?
【问题讨论】: