How to set grid column / row size without defining each row?

I have a grid. I have to define each column and row manually, for example:

<Window x:Class="GridBuild" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="GridBuild" Height="300" Width="300"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> </Grid> 

I want to determine the number of rows and columns with one row, something like this:

 <Window x:Class="GridBuild" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="GridBuild" Height="300" Width="300"> <Grid> <Grid.NumberOfRows="2"/> <Grid.NumberOfColumns/> </Grid> </Window> 
+6
source share
4 answers

What you are describing is called UniformGrid . It has the Columns and Rows , with which you can set the number of rows or columns that you want.

If you do not set these properties, UniformGrid will try to position the children as close to the square as possible. In this situation, he prefers to increase the number of columns to increase the number of rows.

This is an obscure panel, but it is extremely effective when used properly.

+7
source

I suggest getting from Grid and adding these properties to it, as shown below:

 public class GridEx : Grid { public int NumberOfRows { get { return RowDefinitions.Count; } set { RowDefinitions.Clear(); for (int i = 0; i < value; i++) RowDefinitions.Add(new RowDefinition()); } } public int NumberOfColumns { get { return ColumnDefinitions.Count; } set { ColumnDefinitions.Clear(); for (int i = 0; i < value; i++) ColumnDefinitions.Add(new ColumnDefinition()); } } } 

Now it can be used as follows:

 <local:GridEx NumberOfRows="3" NumberOfColumns="2"> <TextBox>some text</TextBox> <TextBox Grid.Row="1">some text</TextBox> <TextBox Grid.Row="2">some text</TextBox> <TextBox Grid.Column="1">some text</TextBox> <TextBox Grid.Row="1" Grid.Column="1">some text</TextBox> <TextBox Grid.Row="2" Grid.Column="1">some text</TextBox> </local:GridEx> 

Also works in the designer :)

The task here is to set different widths, heights, etc. for different rows and columns. I have a good idea how to do this. It is also possible to automate the assignment of Grid.Row and Grid.Column. Think about it:)

+4
source

The above EvAlex answer will work, but only if you don't want to set the number of columns / rows using data binding.

 public class GridEx : Grid { public int NumberOfRows { get { return RowDefinitions.Count; } set { RowDefinitions.Clear(); for (int i = 0; i < value; i++) RowDefinitions.Add(new RowDefinition()); } } public int NumberOfColumns { get { return ColumnDefinitions.Count; } set { ColumnDefinitions.Clear(); for (int i = 0; i < value; i++) ColumnDefinitions.Add(new ColumnDefinition()); } } } 

If you want to install them using data binding (like me), then using the above solution, the compiler will complain, because it requires DependencyProperties . A DependencyProperty can be implemented (using the C # 6 nameof ) as follows (a quick way to insert it using the propdp fragment):

 public int Columns { get { return (int) GetValue(ColumnsDependencyProperty); } set { SetValue(ColumnsDependencyProperty, value); } } public static readonly DependencyProperty ColumnsDependencyProperty = DependencyProperty.Register(nameof(Columns), typeof(int), typeof(GridEx), new PropertyMetadata(0)); 

But in this way, you cannot follow the necessary logic to add the required number of RowDefinitions . To solve this problem, define a DependencyPropertyDescriptor for each DependencyProperty and add the AddValueChanged call with the necessary logic to it in your own class constructor. Then the result on the property (using the null conditional operator C # 6 ?. ):

  public int Columns { get { return (int) GetValue(ColumnsDependencyProperty); } set { SetValue(ColumnsDependencyProperty, value); } } public static readonly DependencyProperty ColumnsDependencyProperty = DependencyProperty.Register(nameof(Columns), typeof(int), typeof(GridEx), new PropertyMetadata(0)); DependencyPropertyDescriptor ColumnsPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(ColumnsDependencyProperty, typeof(GridEx)); public GridEx() { ColumnsPropertyDescriptor?.AddValueChanged(this, delegate { ColumnDefinitions.Clear(); for (int i = 0; i < Columns; i++) ColumnDefinitions.Add(new ColumnDefinition()); }); } 
+3
source

Look at the helper who does exactly what you ask: https://rachel53461.wordpress.com/2011/09/17/wpf-grids-rowcolumn-count-properties/

If you added an assistant from the link above to your project, you can use it as follows:

 <Grid local:GridHelpers.RowCount="6" local:GridHelpers.StarRows="5" local:GridHelpers.ColumnCount="4" local:GridHelpers.StarColumns="1,3"> </Grid> 
+1
source

Source: https://habr.com/ru/post/915586/


All Articles