4

I'm trying to add items programmaticly to a DataGrid and letting the user edit the data. However, I keep getting "EditItem is not allowed for this view" errors when trying to edit data. I tried making the class I'm adding an ObservableCollection but it doesn't seem to change the errors. These are snippets of my code:

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 adding the item:

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

Example class:

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;
    }
}
Fedor
  • 1,548
  • 3
  • 28
  • 38
Deniz Zoeteman
  • 9,691
  • 26
  • 70
  • 97
  • 1
    Your not suppose to use ObservableCollection in this way , you need to have an ObservableCollection not for each item to be an ObservableCollection it self . – eran otzap Nov 17 '13 at 15:04
  • @eranotzap How would I do this? (could you give an example for the class and how to add an Example item?) – Deniz Zoeteman Nov 17 '13 at 15:20
  • Have you tried to attach an BeginningEdithandler and set e.Cancel = true ? This worked fine for me. Please note that this answer is not my own, its from @ouflak -> http://stackoverflow.com/questions/3002063/edititem-is-not-allowed-for-this-view-databinding-issue – BudBrot Nov 21 '13 at 09:44
  • @DenizZoeteman Exmplae : ObservableCollection did you mean that each Example aside from it's own fields is a collection of other Examples ? – eran otzap Nov 21 '13 at 18:28

1 Answers1

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>

CS: (In your 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 ).
   }
eran otzap
  • 12,293
  • 20
  • 84
  • 139
  • How do I get access to Examples in the MainWindow CS? When I try Examples I get an error. – Deniz Zoeteman Nov 17 '13 at 15:39
  • Examples = New ObservableCollection(); – user2880486 Nov 21 '13 at 18:08
  • @DenizZoeteman Examples is a Property in your MainWindow.cs i'll edit the answer and explain in more detail. – eran otzap Nov 21 '13 at 18:16
  • With your edit answer, I still get the same error unfortunately. – Deniz Zoeteman Nov 22 '13 at 19:42
  • did you change Example class so it's not an ObservaleCollection ? 1) post your current Example class – eran otzap Nov 22 '13 at 19:58
  • I've tried both ways, keeping it an observableCollection and without. Don't think it's necessary to post it again, it's still the same just without it being ObservableCollection. – Deniz Zoeteman Nov 23 '13 at 21:00
  • Never mind, it does seem to work, I just forgot to bind ItemsSource to Examples and add to that. It does work now good now, but it creates an item in the grid on startup of the application. What can I do best to prevent this from happening? – Deniz Zoeteman Nov 23 '13 at 23:19
  • Nvm actually, got it working by adding `CanUserAddRows="False"` to the DataGrid. Thanks for your help. – Deniz Zoeteman Nov 23 '13 at 23:34
  • I didn't understand if you even used any of my answer ? – eran otzap Nov 24 '13 at 08:01
  • No I did. I just forgot to bind the ItemsSource to the Examples collection, and after that I just had a little issue with a row being created on startup, which I fixed with adding `CanUserAddRows="False"`. – Deniz Zoeteman Nov 24 '13 at 15:35