1

I want to be able to dynamically set a window's size in WPF, and then create as many columns and rows in a grid as I want.

I've created a method for it but it does not seem to work. I also added a border in XAML to see if there are columns and rows but I only see one square. Also, it throws no error whatsoever.

This is my method:

public void CreateField(Grid MainGrid, TextBox Columns, TextBox Rows, TextBox WHeight, TextBox WWidth, MainWindow MainWindow)
{
    int ColumnCount = Int32.Parse(Columns.Text);
    int RowCount = Int32.Parse(Rows.Text);
    int WindowHeight = Int32.Parse(WHeight.Text);
    int WindowWidth = Int32.Parse(WWidth.Text);

    MainWindow.MainWindow1.Height = WindowHeight;
    MainWindow.MainWindow1.Width = WindowWidth;

    for(int a = 0; a <= ColumnCount; a++){
        ColumnDefinition c = new ColumnDefinition();
        c.Width = new GridLength(WindowWidth / ColumnCount, GridUnitType.Pixel);                
        MainGrid.ColumnDefinitions.Add(c);          
    }
    for (int a = 0; a <= RowCount; a++)
    {
        RowDefinition r = new RowDefinition();
        r.Height = new GridLength(WindowHeight / RowCount, GridUnitType.Pixel);
        MainGrid.RowDefinitions.Add(r);
    }
}

In XAML I have this good with only 1 column and 1 row and a dockpanel for the textboxes and buttons.

<Border BorderBrush="Black" BorderThickness="2">
    <Grid Name="MainWindowGrid">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Name="DockPanel"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <DockPanel Background="LightSalmon" Grid.Row="0" Grid.Column="0" Grid.RowSpan="8">
            <StackPanel>
                <TextBox Name="txtColums" Text="16"/>
                <TextBox Name="txtRows" Text="8"/>
                <TextBox Name="txtWindowHeight" Text="800"/>
                <TextBox Name="txtWindowWidth" Text="1600"/>
                <Button x:Name="ButtonCreate" Content="Create" Click="ButtonCreate_Click"/>
            </StackPanel>
        </DockPanel>
        <ContentControl Content="{Binding}"/>
    </Grid>
</Border>

enter image description here

And according to the parameters written here the method should be executed on button click. But instead of a grid with 16 columns and 8 rows, I only get 1 column 1 row. (you can see the border at the edge)

So what am I doing wrong here? I have no real experience with grids whatsoever, and I am pretty clueless. Hope someone can help me out.

enter image description here

EDIT:

The suggestion of to activate MainGrid.ShowGridLines as said by ASh worked. Did not know about this functionality. As it turns out i do have succesfully created the columns and rows. I thought it not to work because i tried to paint a field in the grid with a color which did not work. Now i wonder, why this does not work as i thought this to be correct code.

var converter = new System.Windows.Media.BrushConverter();
var brush1 = (Brush)converter.ConvertFromString("#FFFFFFF0");
DockPanel myDockPanel = new DockPanel();
Grid.SetColumn(myDockPanel, 3);
Grid.SetRow(myDockPanel, 3);
myDockPanel.Background = brush1;
8
  • 1
    it is an empty grid. how do you know that there are 1 row / 1 column? set MainGrid.ShowGridLines=true; to see exact layout Commented Feb 28, 2018 at 8:56
  • Try to color at least the background rather than white Commented Feb 28, 2018 at 9:01
  • @Ash thank you! :) But please look at my edit as well Commented Feb 28, 2018 at 9:05
  • @Ugur well if everything was to be blue i would have seen no differene either :) Commented Feb 28, 2018 at 9:06
  • @MansNotHot, again are you sure? #FFFFFFF0 is almost white. and it is on white background Commented Feb 28, 2018 at 9:08

2 Answers 2

3

There was a lot going wrong here and its a magical jar of wonderment why you wanted to do this, however this should point you in a better direction

Some Modifications

  • Dedicated Grid
  • Get the ActualHeight and ActualWidth to use
  • Set GridLines true, so you can see whats happening
  • Set the Grid alignments to Stretch
  • Don't alter the size of the window

Xaml

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Border Grid.Row="0"
            Grid.Column="0"
            BorderBrush="Black"
            BorderThickness="2">
        <DockPanel Grid.RowSpan="8" Background="LightSalmon">
            <StackPanel>
                <TextBox Name="txtColums" Text="16" />
                <TextBox Name="txtRows" Text="8" />
                <TextBox Name="txtWindowHeight" Text="800" />
                <TextBox Name="txtWindowWidth" Text="1600" />
                <Button x:Name="ButtonCreate"
                        Click="ButtonCreate_OnClick"
                        Content="Create" />
            </StackPanel>
        </DockPanel>
    </Border>
    <Grid Name="MainWindowGrid"
          Grid.Row="1"
          HorizontalAlignment="Stretch"
          VerticalAlignment="Stretch"
          ShowGridLines="True" />

</Grid>

Code behind

public void CreateField(Grid MainGrid, TextBox Columns, TextBox Rows) // TextBox WHeight, TextBox WWidth, MainWindow MainWindow)
{
   var ColumnCount = int.Parse(Columns.Text);
   var RowCount = int.Parse(Rows.Text);
   var width = MainGrid.ActualWidth;
   var height = MainGrid.ActualHeight;

   for (var a = 0; a <= ColumnCount; a++)
   {
      var c = new ColumnDefinition();
      c.Width = new GridLength(width / ColumnCount, GridUnitType.Pixel);
      MainGrid.ColumnDefinitions.Add(c);
   }
   for (var a = 0; a <= RowCount; a++)
   {
      var r = new RowDefinition();
      r.Height = new GridLength(height / RowCount, GridUnitType.Pixel);
      MainGrid.RowDefinitions.Add(r);
   }
}

private void ButtonCreate_OnClick(object sender, RoutedEventArgs e)
{
   CreateField(MainWindowGrid, txtColums, txtRows);
}

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for this answer! helped a lot. but for clarification why are you against resizing the window in code?
@MansNotHot Not against , it just makes it easier to understand
1

grid layout is correct. set MainGrid.ShowGridLines=true; to see it

to see dynamically created control you should add it to grid:

MainGrid.Children.Add(myDockPanel);

since grid rows have equal height and columns have equal width, the following lines can be safely removed:

r.Height = new GridLength(WindowHeight / RowCount, GridUnitType.Pixel);
c.Width = new GridLength(WindowWidth / ColumnCount, GridUnitType.Pixel);

if Width/Height is not set, it is defaulted to * which means Grid will size them equally.

as an alternative UniformGrid can be used:

AnotherMainGrid = new UnifromGrid {Rows = RowCount, Columns = ColumnCount };

but in this case child elements must be added consequtively

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.