DataGrid edition 'EditItem' is not allowed for this view` when bound to a WPF DataGrid

后端 未结 8 1581
醉酒成梦
醉酒成梦 2020-12-09 17:03

I\'ve been reading about this at least for 4 hours, and seems to be the list type, but I have a situation:

A ObservableCollection that has a collection property.

8条回答
  •  旧时难觅i
    2020-12-09 17:06

    Tks to @nit who give me the right path. Of course the problem reside on the base Type of EF collections

    Hashet< T > And Datagrid need at least a List< T >, changing all my classes "those generated by Entity framework", give to me another problem, must make changes manually, and I have a lot of them.

    My solution was to create a converter, that made the dirty work for me:

    public class listToObservableCollection : BaseConverter, IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            HashSet observableList = (HashSet)value;
            return new ObservableCollection(observableList);
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (HashSet)value;
        }
    }
    
    public abstract class BaseConverter : MarkupExtension
    {
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }
    

    And put it on the binding of my Datagrid2:

    
    xmlns:l="clr-namespace:Recursos;assembly=Recursos"
    ...
    
    
    ...
    
    ItemsSource="{Binding Level2,Converter={StaticResource listoToObservable}}"
    

    The only thing on the air is how to make a generic converter, but for now it works fine.

提交回复
热议问题