-2

我花了很长时间才弄清楚如果 ItemsSource 不是 ObservableCollection,DataGridView 将不允许编辑单元格。我的假设是对的吗?

我有一个 DataGridView,它具有另一个 DataGridView 的 SelectedItem 的属性作为 ItemsSource。不幸的是,Property 不能是 ObservalbeCollection 或派生自它。

所以我的具体问题是,如果 DataGridView 的 ItemsSource 不是明确的 OberservableCollection,则它不允许我编辑单元格。我希望 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>
4

1 回答 1

0

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

'EditItem' is not allowed for this view绑定到 WPF DataGrid 时包装的 ObservableCollection 抛出

于 2013-09-21T01:25:52.380 回答