'EditItem' is not allowed for this view` when editing a detail item

你。 提交于 2019-12-25 07:49:48

问题


Context

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:

Code behind

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;
}

View model

    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 - Detail

<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>

Issue

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`".

Question

How do I stop this exception?


回答1:


How do I stop this exception?

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.



来源:https://stackoverflow.com/questions/41088750/edititem-is-not-allowed-for-this-view-when-editing-a-detail-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!