Reputation: 2421
I am somewhat new to WPF and hopefully I'm not asking for the world here but I'm looking for advice/direction on how to go about implementing something like the following.
I'd like to have my MainWindow
contain N buttons. Each Button
performs the same action on a different set of data (i.e. print picture 1, print picture 2, ... , print picture N). I'd like my window to automatically layout the buttons as described below:
Note how the number of buttons increases, the layout automatically adjusts in a pleasing manner. Up to 6, and then the it provides a horizontal scroll to shuffle through the buttons.
I feel like the <Grid>
control might be the way to provide this but I'm lost in how to get the automatic layout tweaks short of a lot of brute fore.
Tangentially, I see the power in Data Binding in WPF and ideally the button's info (it's display text, graphic, etc.) would be automatically binded to an observable collection so that as I insert buttons into the collection, the UI automatically updates. Conversely when each button is clicked, I'd like to have a generic handler know button 5 maps to the 5th element in my collection which has all this additional info (i.e. the file name of the picture to print).
That all sounds well and good but again I'm lost a bit in the implementation.
Upvotes: 3
Views: 641
Reputation: 1113
As Allonym said, the most customizable way would be to create a new custom Panel
for this.
IMHO, it may also be possible to achieve this using a UniformGrid
, and tweaking it a bit with bindings and converters. That is for the layouting.
About your second question, I think using an ItemsControl
is the best way. You could pass it your new Panel
(or UniformGrid
) as its ItemsPanel
. Also, you could then create a DataTemplate
with a button inside, bind its Command
property to a single command (= generic Handler), with as parameter the DataContext
of the DataTemplate
(= the current item of the list). This part is easier than the layouting.
Does is help?
Antoine
Upvotes: 1