Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在wpf网格中设置全局行定义_C#_Wpf_Layout_Grid_Rowdefinition - Fatal编程技术网

C# 在wpf网格中设置全局行定义

C# 在wpf网格中设置全局行定义,c#,wpf,layout,grid,rowdefinition,C#,Wpf,Layout,Grid,Rowdefinition,我有一个有两个标签的网格 <Grid> <Label Grid.Row="0" Content="Hello" Height="20"></Label> <Label Grid.Row="1" Content="World" Height="20"></Label> </Grid> 我知道我可以为每一行添加,但是如果我有100行,我需要添加100行定义。 是否有一种全局设置行定义的方法,以便所有行/列都

我有一个有两个标签的网格

<Grid>
    <Label Grid.Row="0" Content="Hello" Height="20"></Label>
    <Label Grid.Row="1" Content="World" Height="20"></Label>
</Grid>

我知道我可以为每一行添加,但是如果我有100行,我需要添加100行定义。
是否有一种全局设置行定义的方法,以便所有行/列都继承其属性?

您始终可以将
网格子类化。在加载控件时,手动设置每个子控件的
Grid.Row
属性,并添加行定义。您可以向控件添加属性,以为每行指定统一的高度。例如:

public class UniformGrid : Grid
{
    public double? UniformHeight { get; set; }

    public UniformGrid()
    {
        Loaded += UniformGrid_Loaded;
    }

    void UniformGrid_Loaded(object sender, RoutedEventArgs e)
    {
        int i=0;
        foreach (FrameworkElement child in Children)
        {
            var rowHeight = UniformHeight.HasValue ? new GridLength(UniformHeight.Value) : GridLength.Auto;
            RowDefinitions.Add(new RowDefinition { Height = rowHeight });
            Grid.SetRow(child, i++);
        }
    }
}
用法:

<my:UniformGrid UniformHeight="20">
    <Label Content="Hello" />
    <Label Content="World" />
</my:UniformGrid>


(虽然,正如一些评论所指出的,上述基本上只是一个堆栈面板,速度较慢。)

但如果我有100行,我需要添加100行定义
-您不需要在
网格中放置100行。没有必要这样做,您需要12938042309842304的屏幕分辨率
1209381203120398
来显示这一点。这没有意义。你可以用StackPanel代替它,周围有一个ScrollViewer…好吧,即使我有4-5行,有没有办法全局设置行高?但是StackPanel会填充网格中的整个宽度吗?或者也许WrapPanel会起作用。。。