0


While working in WPF i have the need for a Dynamic Grid. By this i mean a grid that contains only one kind of object, has a template for that object etc. But unlike a similar ItemsControl like a Listbox, i want the grid to be given a Maximum Columns property. This should act as a delimiter which will then calculate the number of rows needed based on the number of objects within the grid. To do this, i thought of inherriting a Grid to make use of its Row and Column properties, but i have a problem... I dont know how to implement an ItemsSource property outside of inherriting the ItemsSource from an ItemsControl...

so my question comes in two parts...

  1. Am i pursuing this the right way? should i be inherriting ItemsControl and trying to re-implement the Grid behavior
  2. if this is the right way to do it, how do i implement an ItemsSource property with its corresponding ItemTemplate

2 Answers 2

3

Perhaps a better way would be to use a ListView? Here is an example how to achieve 3-column output: http://kristofmattei.be/2010/03/16/multi-column-listview/

1

Do you want something like UniformGrid? If you set the Columns property (and don't set the Rows property), it will automatically figure out how many rows to create to hold its items.

4
  • Your answer and Vlad's are both similar in that they use different implementations of a UniformGrid. I will try them both out, but Thank you! i didnt know of this controls existence Oct 25, 2010 at 0:49
  • UniformGrid doesn't seem to offer features like ItemsSource.
    – Vlad
    Oct 25, 2010 at 0:52
  • That is the reason i gave yours the tick :) UniformGrid is used in both, and supplies most of the features i want, but blending it with a Listbox provides all the functionality i need Oct 25, 2010 at 0:57
  • Yes, I should have thought of that in my answer. You can't bind directly to a UniformGrid; you have to set it as the ItemsPanel of an ItemsControl. See the examples at msdn.microsoft.com/en-us/library/… but use a UniformGrid instead of a StackPanel.
    – Joe White
    Oct 25, 2010 at 1:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.