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
WPF将许多项加载到网格并显示在新的可用行/列上_Wpf_Grid - Fatal编程技术网

WPF将许多项加载到网格并显示在新的可用行/列上

WPF将许多项加载到网格并显示在新的可用行/列上,wpf,grid,Wpf,Grid,我在WPF应用程序上创建了一个网格,其中有4行和4列用于显示媒体: <Grid Name="ControlsGrid"> <Grid.ColumnDefinitions> <ColumnDefinition Width="33*" /> <ColumnDefinition Width="33*" /> <Co

我在WPF应用程序上创建了一个网格,其中有4行和4列用于显示媒体:

        <Grid Name="ControlsGrid">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="33*" />
                <ColumnDefinition Width="33*" />
                <ColumnDefinition Width="33*" />
                <ColumnDefinition Width="33*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="33*" />
                <RowDefinition Height="33*" />
                <RowDefinition Height="33*" />
                <RowDefinition Height="33*" />
            </Grid.RowDefinitions>
        </Grid>
对象加载良好,在调试期间,我看到集合的大小增加。。但该控件显示在第一个0,0网格上,添加新控件时,它将覆盖该控件。

如何在网格上的空白处设置控件?

您需要一种方法来确定下一个空白,然后需要设置网格行和列附着的属性:

        Grid.SetRow(control, row);
        Grid.SetColumn(control, column);

您只需为每个空间中的控件设置
网格.Column
网格.Row

但是,我注意到您将所有网格列/行设置为相同的大小,因此UniformGrid可能是更好的选择

Xaml:


代码:

公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
AddWinformControls();
}
私有void AddWinformControls()
{
对于(int i=0;i<12;i++)
{
WindowsFormsHost formhost=新WindowsFormsHost();
formhost.Child=new System.Windows.Forms.Label(){Text=“Hello”};
ControlsGrid.Children.Add(formhost);
}
}
}

那么您想在每个网格列/行中添加一个新的WinformsHost?另外,
VideosGrid
ControlsGrid
是不同的
Grid
吗?谢谢,另外,你知道WindowsFormsHost是否只适合一个控件还是应该将所有控件添加到它的子控件中。控件?我从未使用过WindowsFormsHost,所以我不确定对不起
        Grid.SetRow(control, row);
        Grid.SetColumn(control, column);
<Window x:Class="WpfApplication16.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Name="UI">
    <UniformGrid Name="ControlsGrid" Rows="4" Columns="4" />
</Window>
public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
        AddWinformControls();
    }

    private void AddWinformControls()
    {
        for (int i = 0; i < 12; i++)
        {
            WindowsFormsHost formhost = new WindowsFormsHost();
            formhost.Child = new System.Windows.Forms.Label() { Text = "Hello" };
            ControlsGrid.Children.Add(formhost);
        }
    }
}