上下文
使用Entity Framework 6我有一个Airframe对象的ObservableCollection,每个对象都有一个Identity对象的从属集合。通过XAML和一个视图模型,我一次在一个DataGrid中查看每个机身(主机)的一个机身(主机)。为了一次只处理一个机身的显示/更新,我正在使用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 - Master
<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中的任何行时,第一次单击选择单元格,第二次单击导致异常“在带有'EditItem'的PresentationFramework.dll中发生未处理的类型'System.InvalidOperationException'异常不允许这个观点`“。
问题
如何停止此例外?
答案 0 :(得分:1)
如何停止此例外?
您需要确保“身份”属性的类型支持编辑。它应该实现IList接口。的HashSet&LT; T&GT;例如,但不是List&lt; T&gt;和ObservableCollection&lt; T&gt;做。