编辑DataGridViewCell而不将ObservableCollection作为ItemsSource

时间:2013-09-21 01:12:29

标签: c# wpf datagridview observablecollection inotifycollectionchanged

如果ItemsSource不是ObservableCollection,我花了很长时间才弄清楚DataGridView不允许编辑Cell。我在这个假设中是对的吗?

我有一个DataGridView,它具有另一个DataGridView的SelectedItem属性作为ItemsSource。不幸的是,财产不能是ObservalbeCollection或从中衍生出来。

所以我的具体问题是,如果DataSridView的ItemsSource没有明确地是OberservableCollection,那么DataGridView将不允许我编辑Cell。我希望,因为这个原因,INotifyCollectionChanged接口就在那里。有什么建议吗?

提前致谢

约翰

这是我的ViewModel的代码,用作ItemsSource

public class NdfCollection : NdfValueWrapper, IList<NdfValueWrapper>, INotifyCollectionChanged
{
    private readonly ObservableCollection<NdfValueWrapper> _innerList = new ObservableCollection<NdfValueWrapper>();

    public NdfCollection(long offset)
        : base(NdfType.List, offset)
    {

    }

    public NdfCollection(IEnumerable<NdfValueWrapper> list, long offset)
        : this(offset)
    {
        if (list != null)
            foreach (NdfValueWrapper wrapper in list)
                InnerList.Add(wrapper);
    }

    public ObservableCollection<NdfValueWrapper> InnerList
    {
        get { return _innerList; }
    }

    // Implementation of the Interfaces not pasted in here
}

这是我的DataGrid的XAML代码

    <DataGrid Grid.Row="1" MaxHeight="400" ItemsSource="{Binding Path=SelectedItem.Value,  ElementName=propGrid}" 
                  IsSynchronizedWithCurrentItem="True"
                  CanUserResizeRows="False"
                  CanUserAddRows="False"  
                  CanUserDeleteRows="False" 
                  AutoGenerateColumns="False"
                  SelectionMode="Single"
                  SelectionUnit="CellOrRowHeader" IsReadOnly="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=Type}" Header="Type" IsReadOnly="True" Width="*" />
            <!--<DataGridTextColumn Binding="{Binding}" Header="Biniary Value" IsReadOnly="False" Width="*" />-->
            <DataGridTemplateColumn Header="Value" IsReadOnly="False" Width="*" CellEditingTemplateSelector="{DynamicResource editingControlTemplateSelector}" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

1 个答案:

答案 0 :(得分:0)

解决方案是将IList显式实现到自定义Collection。作品!

Wrapped ObservableCollection throwing 'EditItem' is not allowed for this view when bound to a WPF DataGrid