【问题标题】:Edit DataGridViewCell without having an ObservableCollection as ItemsSource在没有 ObservableCollection 作为 ItemsSource 的情况下编辑 DataGridViewCell
【发布时间】:2013-09-21 01:12:29
【问题描述】:

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

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

所以我的具体问题是,如果 DataGridView 的 ItemsSource 不是明确的 OberservableCollection,则不允许我编辑单元格。我希望 INotifyCollectionChanged 接口是因为这个原因而存在的。有什么建议吗?

提前致谢

约翰

这是用作 ItemsSource 的 ViewModel 的代码

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>

【问题讨论】:

    标签: c# wpf datagridview observablecollection inotifycollectionchanged


    【解决方案1】:

    解决方案是显式实现 IList 到自定义 Collection。有效!

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-13
      • 2013-03-29
      • 1970-01-01
      • 1970-01-01
      • 2012-06-07
      相关资源
      最近更新 更多