编辑明细项目时,此视图不允许使用“ EditItem” [英] 'EditItem' is not allowed for this view` when editing a detail item

查看:74
本文介绍了编辑明细项目时,此视图不允许使用“ EditItem”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文

使用实体框架6我有一个Airframe对象的ObservableCollection,每个对象都有一个Identity对象的从属集合。通过XAML和视图模型,我一次查看一个飞机架(主机),并在DataGrid中一次查看其每个Identity(详细信息)对象。为了一次只显示/更新一个机身,我正在使用CollectionView,这样我就可以在集合中获得当前位置,并可以连接转到下一个和转到上一个按钮和命令。简化的代码摘录:

Using Entity Framework 6 I have an ObservableCollection of Airframe objects each of which has a subordinate collection of Identity objects. Through XAML and a view model I'm viewing this collection one airframe (master) at a time with each of it's Identity (detail) objects in a DataGrid. To handle the display / update of just one Airframe at a time I'm using a CollectionView so that I get current positioning within the collection and can wire up "Goto next" and "Goto previous" buttons and commands. A simplified code extract:

后面的代码

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>

问题

这将完美显示数据,我可以毫无问题地编辑主数据。当我单击详细数据网格中的任何行时,但是第一次单击选择了单元格,第二次单击导致了异常不允许在PresentationFramework.dll中使用'EditItem'发生类型为'System.InvalidOperationException'的未处理异常

This displays the data perfectly and I can edit the master data without a problem. When I click into any of the rows in the detail DataGrid however the first click selects the cell and the second click causes an exception "An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll with 'EditItem' is not allowed for this view`".

问题

如何阻止此异常?

推荐答案


如何阻止此异常?

How do I stop this exception?

您需要确保身份属性的类型支持编辑。它应该实现IList接口。 HashSet< T>例如,List< T>和ObservableCollection< T>做。

You need to make sure that the type of your "Identities" property supports editing. It should implement the IList interface. HashSet<T> does not for example but List<T> and ObservableCollection<T> do.

这篇关于编辑明细项目时,此视图不允许使用“ EditItem”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆