将子项附加到网格,设置它的行和列 [英] Append a child to a grid, set it's row and column

查看:23
本文介绍了将子项附加到网格,设置它的行和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 Image 对象附加到 Grid 中并将其设置为 RowColumn?

网格是 3x3.

主文件:

<窗口.背景><ImageBrush ImageSource="C:\Users\GuyD\AppData\Local\Temporary Projects\WpfApplication1\AppResources\Background.png"></ImageBrush></Window.Background><Grid ShowGridLines="True" x:Name="myGrid"><Grid.RowDefinitions><RowDefinition Height="42"/><RowDefinition Height="30*"/><RowDefinition Height="30*"/><RowDefinition Height="32*"/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="31*"/><ColumnDefinition Width="26*"/><ColumnDefinition Width="32*"/></Grid.ColumnDefinitions></网格></窗口>

文件隐藏代码:

public MainWindow(){初始化组件();for (int i = 0; i <3; i++){for (int j = 0; j <3; j++){Image Box = new Image();this.myGrid.Children.Add(Box);}}}

解决方案

Grid setter 方法是静态的.
将它们放在第 1 行第 1 列中:

Image Box = new Image();myGrid.Children.Add(Box);Grid.SetRow(Box, 1);Grid.SetColumn(Box, 1);

How can I append an Image object into a Grid and set it's Row and Column?

The grid is 3x3.

Main file:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="440" Width="400" ResizeMode="NoResize">
    <Window.Background>
        <ImageBrush ImageSource="C:\Users\GuyD\AppData\Local\Temporary Projects\WpfApplication1\AppResources\Background.png"></ImageBrush>
    </Window.Background>
    <Grid ShowGridLines="True" x:Name="myGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="42" />
            <RowDefinition Height="30*" />
            <RowDefinition Height="30*" />
            <RowDefinition Height="32*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="31*" />
            <ColumnDefinition Width="26*" />
            <ColumnDefinition Width="32*" />
        </Grid.ColumnDefinitions>
    </Grid>
</Window>

Code behind file:

public MainWindow()
{
     InitializeComponent();
     for (int i = 0; i < 3; i++)
     {
          for (int j = 0; j < 3; j++)
          {
               Image Box = new Image();
               this.myGrid.Children.Add(Box);
          }
     }
}

解决方案

The Grid setter methods are static.
To place them in row 1 column 1:

Image Box = new Image();
myGrid.Children.Add(Box);
Grid.SetRow(Box, 1);
Grid.SetColumn(Box, 1);

这篇关于将子项附加到网格,设置它的行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆