0

I'm writing a program that is a simple puzzle game - all but one cells are filled with buttons and you have to order them properly by moving them around using the only one empty cell. The first version was a 3x3 grid with 8 buttons + one invisible. However, now I want to enable users to change the number of rows and columns. I need to use sliders to manipulate height and width. Unfortunately, I have no idea how can I manipulate the grid during runtime. Here's a screenshot of what I have at the moment: screenshot

1 Answer 1

2

Unless WPF is far different from Silverlight, can't you just programmatically alter the column/grid definitions?

(just warning, writing this in notepad as I don't have visual studio with me to check, so I'm just going by memory)

myGrid.Columns.Clear(); myGrid.Rows.Clear(); int buttonNumber = 0; double buttonWidth = GridWidth / numberOfColumns; double buttonHeight = GridHeight / numberOfRows; for (int columnNumber = 0; columnNumber < numberOfColumns; columnNumber++) { var column = new ColumnDefinition(); myGrid.Columns.Add(column); for (int rowNumber = 0; rowNumber < numberOfRows; rowNumber++) { var row = new RowDefinition(); myGrid.Rows.Add(row); var button = new Button(); button.Content = ++buttonNumber; button.Width = buttonWidth; button.Height = buttonHeight; Grid.SetColumn(button, columnNumber); Grid.SetRow(button, rowNumber); } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately, I think there are no such methods as Clear() or Add() in Grid class in WPF.
@vtvs On the Grid class, no. It's on the ColumnDefinitionCollection and RowDefinitionCollection classes though which is what I have: msdn.microsoft.com/en-us/library/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.