这个视图不允许进行EditItem操作。

4
我将尝试以编程方式向DataGrid添加项目,并允许用户编辑数据。但是,当尝试编辑数据时,我不断收到“此视图不允许EditItem”错误提示。我尝试将我添加的类变为ObservableCollection,但似乎并没有改变错误。以下是我的代码片段:
XAML:
<DataGrid x:Name="DataGridExample" HorizontalAlignment="Left" Margin="35,40,0,0" VerticalAlignment="Top" Height="220" Width="525" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=Name}" ClipboardContentBinding="{x:Null}" Header="Filename"/>
                <DataGridTextColumn Binding="{Binding Path=Prefix}" ClipboardContentBinding="{x:Null}" Header="Prefix"/>
                <DataGridCheckBoxColumn Binding="{Binding Path=Sign}" ClipboardContentBinding="{x:Null}" Header="Sign"/>
                <DataGridCheckBoxColumn Binding="{Binding Path=Bin}" ClipboardContentBinding="{x:Null}" Header="Bin"/>
                <DataGridTextColumn Binding="{Binding Path=FolderPath}" ClipboardContentBinding="{x:Null}" Header="Folderpath"/>
            </DataGrid.Columns>
</DataGrid>

MainWindowClass 添加该项:

Example newExample = new Example() { FolderPath = folderpath, Name = foldername, Prefix = foldername, Bin = false, Sign = false };
DataGridExample.Items.Add(newExample);

示例类:

public class Example : ObservableCollection<Example>
{
    public string FolderPath { get; set; }

    public string Name { get; set; }

    public string Prefix { get; set; }

    public bool Sign { get; set; }

    public bool Bin { get; set; }

    public override string ToString()
    {
        return Name;
    }
}

1
你不应该以这种方式使用ObservableCollection,你需要有一个ObservableCollection<Example>,而不是每个项都是一个ObservableCollection。 - eran otzap
@eranotzap,我该怎么做?(您能给出一个类和如何添加一个示例项目的例子吗?) - Deniz Zoeteman
你尝试过附加BeginningEdithandler并设置e.Cancel = true吗?这对我很有效。请注意,这个答案不是我的,而是来自@ouflak->https://dev59.com/Y07Sa4cB1Zd3GeqP2VDv - BudBrot
@DenizZoeteman 示例:ObservableCollection<Example>你的意思是每个Example除了它自己的字段外,还是其他Examples的集合吗? - eran otzap
1个回答

3

xaml :

   <DataGrid ItemsSource="{Binding Examples}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=Name}" Header="Filename"/>
            <DataGridTextColumn Binding="{Binding Path=Prefix}" Header="Prefix"/>
            <DataGridCheckBoxColumn Binding="{Binding Path=Sign}" Header="Sign"/>
            <DataGridCheckBoxColumn Binding="{Binding Path=Bin}" Header="Bin"/>
            <DataGridTextColumn Binding="{Binding Path=FolderPath}" Header="Folderpath"/>
        </DataGrid.Columns>
   </DataGrid>

在您的MainWindow.cs文件中:

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this; // By setting itself to be it's own DataContext 
     // i can easily  bind to properties in my code behind (There are other ways but this is the most               correct and easy one.)
    }


   private ObservableCollection<Example> _examples;
   public ObservableCollection<Example> Examples
   {
       get
       {
          if(_examples == null)
               _examples = new ObservableCollection<Example>();
          return _examples; 
       } 
   }

   public void OnAddingItem()
   {
       Example newExample = new Example() { FolderPath = folderpath, Name = foldername, Prefix = foldername, Bin = false, Sign = false };
       Examples.Add(newExample); // Because Examples is an ObservableCollection it raises a    
    //CollectionChanged event when adding or removing items,
    // the ItemsControl (DataGrid) in your case corresponds to that event and creates a new container for the item ( i.e. new DataGridRow ).
   }

我如何访问MainWindow CS中的示例?当我尝试使用示例时,会出现错误。 - Deniz Zoeteman
@DenizZoeteman,Examples是你的MainWindow.cs中的一个属性。我会编辑答案并详细解释。 - eran otzap
很抱歉,使用您提供的编辑答案后,我仍然遇到了相同的错误。 - Deniz Zoeteman
我已经尝试过两种方式,一种是保持它为ObservableCollection,另一种则不是。我认为没有必要再发布一遍了,因为它仍然是相同的,只是没有使用ObservableCollection。 - Deniz Zoeteman
没关系,它似乎可以工作了,我只是忘记将ItemsSource绑定到Examples并添加到其中。现在它可以很好地工作了,但它会在应用程序启动时创建一个网格项。我该怎么做才能最好地防止这种情况发生? - Deniz Zoeteman
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接