-1

I found this tutorial Using a Grid as the Panel for an ItemsControl but I couldn't get it to work.

I get this exception:

'Cannot explicitly modify Children collection of Panel used as ItemsPanel for ItemsControl. ItemsControl generates child elements for Panel.'

caused by grid.Children.Add(child) in this loop

foreach (FrameworkElement child in phantom.Children.ToList())
         {
           phantom.Children.Remove(child);
           grid.Children.Add(child);
           // ensure the child maintains its original datacontext
           child.DataContext = phantom.DataContext;
         }

Is there any work around possible by inheriting from ItemsPanel?

Tom Huntington
  • 2,260
  • 10
  • 20
  • Does this answer your question? [Add n rectangles to canvas with MVVM in WPF](https://stackoverflow.com/questions/22324359/add-n-rectangles-to-canvas-with-mvvm-in-wpf) – Peter Duniho Jul 09 '20 at 21:53
  • 1
    See proposed duplicate for example of how to _correctly_ use `ItemsControl`. Unfortunately, your question is unclear; other than the fact that you are trying to do something which is _explicitly forbidden_, there's no explanation as to _why_ you think this would be useful, nor why you aren't using a correct approach to manipulate the child elements of an `ItemsControl` object. – Peter Duniho Jul 09 '20 at 21:54
  • Well my question was how to override something explicitly forbidden, but I would be happy with any answer that gets the tutorial working. My specific application is unclear because I didn't think it would be relevant. This is useful in its own right because you would be able to use "the most useful panel" (a grid according to the tutorial) with an `ItemsControl` – Tom Huntington Jul 09 '20 at 22:40
  • Perhaps I could just use a stackpanel, but I've already written the code for a grid and a stackpanel would be more hackier because I'd have to coordinate grandchildren. Perhaps I could look more into datatemplates too. But I'm not wanting to know how to solve my problem, but how (whether) I can get this prospective solution to work – Tom Huntington Jul 09 '20 at 22:53
  • 2
    I haven't reviewed it thoroughly, but I question the basic premise of the article you're trying to follow. The `Grid` container is poorly suited to scenarios with variable numbers of children, for reasons other than lack of implicit support for it via `ItemsControl`. Even if one did want to shoehorn `Grid` into an `ItemsControl`, the approach followed by that "tutorial" is IMHO questionable. `ItemsControl` generates `ItemContainer` objects, and the style for the container is where one would do things like set attached properties. ... – Peter Duniho Jul 09 '20 at 22:53
  • 1
    ... Of course, the fact that it simply doesn't even work would be another reason to seek advice from someone other than the author of that article. Rather than asking how to get a bad tutorial to work, you should be focusing on whatever specific real-world problem led you to the tutorial in the first place, and ask for help with _that_. Make sure you include sufficient detail, including a [mcve]. – Peter Duniho Jul 09 '20 at 22:53

1 Answers1

0

Shoehorning Grid into an ItemsControl is not a good idea.

You loose the function of an ItemsControl. Peter Duniho mentioned in the comments that an ItemContainer is where you see attached properties. For my application I needed to use the Selector functionality. If I used a Grid I would have had to implement this myself but I saved myself a lot of trouble just using an Listbox.

If you where wanting to use a Grid I suggest you look into the ListView as well as GridView. And if this isn't flexible enough you can always write your own ViewBase.

Tom Huntington
  • 2,260
  • 10
  • 20