I have a WrapPanel, And I want to specify the Max number of its columns. So, for example, when my Collection "ObjectCollection" (binded to this WrapPanel) contains only 4 elements, the WrapPanel will have only one row. But, when "ObjectCollection" will have 5 elements, the wrapPanel will create another row to put the fifth one. (My Max_Columns_Number in this case is 4).
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
You can control the number of columns by setting the width of the wrap panel. I bind the width of the wrap panel to the ActualWidth of a container like Border. That way, the number of columns is dynamic and is based on the width of the window.
Basically you're going to need to create a custom
Panel
for yourself... now don't get despondent... it's not that difficult. To start with, please take a look at the posts that I have provided links for that explain how to create a customPanel
:How to create a Custom Layout Panel in WPF
Creating Custom Panels In WPF
Ok, so now that you know a bit more about creating custom
Panel
s, we can continue... here's what you're going to need:This property would be used where you declare your
Panel
inResources
:Note that you will need to declare it inside an
ItemsPanelTemplate
object because that is what theItemsPanel
property expects:Now back to the
Panel
... here is a helper method that I call from theMeasureOverride
andArrangeOverride
methods:Unfortunately for you, I cannot provide you with a complete example because my custom
Panel
s are all tied into a baseAnimatedPanel
class with lots of additional functionality. However, you only need to create theMeasureOverride
andArrangeOverride
methods to complete thisPanel
. If you just think logically about it, it's really not that difficult.I'm pretty sure you can't do it with a WrapPanel, but you can use the UniformGrid instead.
That one has properties to specify the number of rows and columns you want.
If you set the
Columns
property to 4, it will keep 4 items in each row, and then wrap to the next one.Sometimes the UniformGrid is not enough:
In this stackoverflow post can be found a WrapPanel with what you are searching for.
Xaml:
Result: