C# 如何通过wpf中的行和列获取网格子项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12195629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:25:24  来源:igfitidea点击:

how to get grid children by its row and column in wpf?

c#wpfxamlgrid

提问by Chandru A

<Grid x:Name="LayoutRoot" HorizontalAlignment="Center" VerticalAlignment="Center" >
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <Button Width="150" Height="50" x:Name="Btn1" Content="Button1" Grid.Row="0" Grid.Column="0"/>
    <Button Width="150" Height="50" x:Name="Btn2" Content="Button2" Grid.Row="0" Grid.Column="1"/>
    <Button Width="150" Height="50" x:Name="Btn3" Content="Button3" Grid.Row="2" Grid.Column="0"/>
    <Button Width="150" Height="50" x:Name="Btn4" Content="Button4" Grid.Row="2" Grid.Column="1"/>
</Grid>

C# code in wpf

wpf中的C#代码

Visual childVisual = (Visual)VisualTreeHelper.GetChild(LayoutRoot,0);

With above code i can get the First child of the grid(LayoutRoot).But i want to get grid child by it's rows or columns. What should i do for that.

使用上面的代码,我可以获得网格的第一个子节点(LayoutRoot)。但是我想通过它的行或列来获取网格子节点。我该怎么做。

Thanks in Advance.

提前致谢。

回答by H.B.

Filter the Grid.Childrenbased on what Grid.GetRowand GetColumnreturns for every child.

Grid.Children根据每个孩子的内容Grid.GetRowGetColumn回报过滤。

e.g.

例如

var itemsInFirstRow = LayoutRoot.Children
                          .Cast<UIElement>()
                          .Where(i => Grid.GetRow(i) == 0);