0
votes

I want to make a Button Tablet, which consists of 1 .. 10 Buttons.

Now I have a grid, which has rows and colums, lets say 2 columns, 5 rows, but this is not very dymamical.

I have some xml-source which specifies which and how much buttons are in that grid, how they should look like, row and column position.

How can I dynamically insert buttons at positions?

In the TabletViewModel I have a collection of ButtonViewModels and it is the DataContext for the TabletView (the Grid)

The ButtonViewModel holds the text, image-filename, row and column of the buttons and is the DataContext for one Button.

I thought of something like the itemsource Binding, but I can't figure out how to do this Binding with a grid and buttons. (Dynamic GridView?)

How can i do this? Any ideas appreciated!

2

2 Answers

1
votes

Or maybe consider using ItemsControl with WrapPanel

<ItemsControl>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>
0
votes

Try this

(assuming x:Name="myGrid" in markup)

// add rows to grid (it's the same deal for columns too)
 for (int row=0;row < numRows; row++)
 {
 myGrid=RowDefinitions.Add(new RowDefinition());
 }

(I'd be interested to see if there is a slicker way of doing this bit. )


// Stick a control in intended location

b = new Button();

myGrid.Children.Add(b);
 Grid.SetRow(b,1); // 2nd row
 Grid.SetColumn(b,3); // 4th column
 Grid.SetColumnSpan(b,2);