tags:

views:

27

answers:

1

I have been using WinForms since the first framework introduced and invested a lot of time and effort in it. Now I am attempting to move to WPF and, honestly, it's not so easy.

Now I have a task, I need to implement a simple wizard, each page of which has a aligned to center group of controls. The group contains a set of buttons, four button in a row. Width of the group is constant, height is variable -- it depends on the number of buttons inside.

alt text

It's a simple task for WinForms, but I have no idea how to do it using XAML.

I have three questions:

1). Obviously, the buttons inside a group is a WrapPanel which is placed in a Grid's cell. It's simple. But how to calculate height of the WrapPanel not using code behind?

2). Which is recommended way to implement wizard? Data template or some kind of Tab Control? I probably will need to have some transition effects when switching pages.

3). Is it acceptable in WPF world to use binding as a way to repositioning controls?

Thank you in advance!

+1  A: 

The WrapPanel will auto-adjust its height based on its contents by default. WPF is a big advancement from WinForms precisely because of the new layout paradigms. No code behind is needed for anything you've mentioned.

As for 2; there are a lot of ways to implement this, depending on how close you adhere to MVVM (if at all); I'd recommend using a styled TabControl at first (you can adjust the style to present visually the steps in the wizard as tabs, without letting the user jump between tabs), as it's easiest. Also, it's possible to bind pretty much everything to the TabControl.

3 is possible, but should be rarely needed. And I mean it.

Now then; a simple example to show you the power of WPF. You can have in your ViewModel (if you're not familiar with MVVM google it or read any of Josh Smith's articles or book or... wow there's such a wealth of information on it I don't know which to choose) a collection of objects, let's say Step classes, which you can bind to the ItemsSource of the TabControl. The TabControl will automatically create a tab for each of your Step. Inside your Step class, you can have a collection of items, let's say... um, Collection<Push> (I'm struggling not to use known classes like Action or Button). If the ItemTemplate of the TabControl contains anything that recognizes that collection, as in a ListBox styled internally to use a WrapPanel for its ItemsContainer, you're done: the template will show the list of Pushes in a WrapPanel and that's that.

Now, I probably shouldn't write a full tutorial here anyway, but that should get you started. Read up on DataTemplates, ItemsControl etc. (again, I'm having difficulties picking resources... Dr. WPF has awesome articles on both, but they might be a bit advanced) and you should be good to go. Just remember that there's a good reason why WPF features a lot more fluid layouts than any previous desktop technology, you should become familiar with that.

Alex Paven
Thank you Alex! I'am familiar with MVVM so i won't be a problem. But I still don't understand how to position the WrapPanel: if it's placed in a Grid's cell then it has fixed height == height of the grid's cell.
Dmitry Karpezo
Actually, it will only have a fixed height if you set it as such. If it's contained in a grid cell and needs more space (you add more items to it), it will request more space and re-layout; if the parent denies the request for more space, it will be capped at the maximum allowed by the parent. This is all complicated by a lot of properties you can set, such as horizontal and vertical alignments, stretch, scroll-related ones etc. and by the layout sub-rules built into each control. However, if you play around in the designer for a while all of this should become pretty obvious.
Alex Paven
Thank you, Alex! You helped a lot!
Dmitry Karpezo