3

メディアを表示するために、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>

そして、別の方法からグリッドに追加します:

            WindowsFormsHost formhost = new WindowsFormsHost();
            formhost.Child = new System.Windows.Forms.Control();
            formhost.Child = control;
            ControlsGrid.Children.Add(formhost);

オブジェクトは正常にロードされています。デバッグ中にコレクションのサイズが大きくなっていることがわかります..しかし、コントロールは最初の 0,0 グリッドに表示され、新しいコントロールを追加すると、そこにあったコントロールが上書きされます。
グリッドの空の場所にコントロールを設定するにはどうすればよいですか?

4

2 に答える 2

1

次の空きスペースを決定する方法が必要になります。次に、グリッドの行と列の添付プロパティを設定する必要があります。

        Grid.SetRow(control, row);
        Grid.SetColumn(control, column);
于 2013-02-14T08:24:38.313 に答える
1

コントロールを各スペースに配置するには、Grid.Columnとを設定するだけです。Grid.Row

ただし、すべてのグリッドの列/行を同じサイズに設定していることに気付いたので、おそらく UniformGrid の方が良いオプションです

Xaml:

<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);
        }
    }
}
于 2013-02-14T08:39:16.907 に答える