7

私はグリッドを持っています。次のように、各列と行を手動で定義する必要があります。

<Window x:Class="GridBuild"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="GridBuild" Height="300" Width="300">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>        
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>        
</Grid>

次のように、行と列の数を 1 行で定義したいと思います。

<Window x:Class="GridBuild"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="GridBuild" Height="300" Width="300">
    <Grid>
        <Grid.NumberOfRows="2"/>
        <Grid.NumberOfColumns/>
    </Grid>
</Window>
4

4 に答える 4

9

あなたが説明するものは と呼ばれUniformGridます。必要な行または列の数を設定できるプロパティがColumnsありRowsます。

これらのプロパティを設定しない場合、UniformGridは子をできるだけ正方形に近づけようとします。この状況では、行数を増やす前に列数を増やすことを優先します。

目立たないパネルですが、正しく使えば非常に強力です。

于 2012-05-13T13:55:07.693 に答える
4

Grid次のように、これらのプロパティから派生して追加することを提案します。

public class GridEx : Grid
{
    public int NumberOfRows
    {
        get { return RowDefinitions.Count; }
        set
        {
            RowDefinitions.Clear();
            for (int i = 0; i < value; i++)
                RowDefinitions.Add(new RowDefinition());
        }
    }

    public int NumberOfColumns
    {
        get { return ColumnDefinitions.Count; }
        set
        {
            ColumnDefinitions.Clear();
            for (int i = 0; i < value; i++)
                ColumnDefinitions.Add(new ColumnDefinition());
        }
    }
}

現在、次のように使用できます。

<local:GridEx NumberOfRows="3" NumberOfColumns="2">
    <TextBox>some text</TextBox>
    <TextBox Grid.Row="1">some text</TextBox>
    <TextBox Grid.Row="2">some text</TextBox>

    <TextBox Grid.Column="1">some text</TextBox>
    <TextBox Grid.Row="1" Grid.Column="1">some text</TextBox>
    <TextBox Grid.Row="2" Grid.Column="1">some text</TextBox>
</local:GridEx>

ちなみにデザイナーでも動作します:)

ここでの課題は、異なる行と列に異なる幅、高さなどを設定することです。私はこれを行う方法について良い考えを持っています。また、Grid.Row と Grid.Column の割り当てを自動化することもできます。考えてみてください:)

于 2012-05-13T09:23:22.370 に答える
3

EvAlex による上記の回答は機能しますが、データ バインディングを使用して列/行の数を設定したくない場合に限ります。

public class GridEx : Grid
{
    public int NumberOfRows
    {
        get { return RowDefinitions.Count; }
        set
        {
            RowDefinitions.Clear();
            for (int i = 0; i < value; i++)
                RowDefinitions.Add(new RowDefinition());
        }
    }

    public int NumberOfColumns
    {
        get { return ColumnDefinitions.Count; }
        set
        {
            ColumnDefinitions.Clear();
            for (int i = 0; i < value; i++)
                ColumnDefinitions.Add(new ColumnDefinition());
        }
    }
}

データバインディングを介してこれらを設定したい場合(私のように)、上記のソリューションでは、コンパイラーはそれが必要なDependencyPropertiesため文句を言います。Aは次のように ( DependencyPropertyC# 6 のnameof演算子を使用して) 実装できます (挿入する簡単な方法は、スニペット propdp を使用することです)。

public int Columns
{
    get { return (int) GetValue(ColumnsDependencyProperty); }
    set { SetValue(ColumnsDependencyProperty, value); }
}
public static readonly DependencyProperty ColumnsDependencyProperty =
    DependencyProperty.Register(nameof(Columns), typeof(int), typeof(GridEx), new PropertyMetadata(0));

しかし、この方法では、必要な数の を追加するために必要なロジックを実行できませんRowDefinitions。これを解決するには、DependencyPropertyDescriptorfor eachを定義し、必要なロジックを含む呼び出しをカスタム クラスのコンストラクターにDependencyProperty追加します。AddValueChangedプロパティごとの結果は次のようになります (C# 6 の null 条件演算子を使用?.):

    public int Columns
    {
        get { return (int) GetValue(ColumnsDependencyProperty); }
        set { SetValue(ColumnsDependencyProperty, value); }
    }
    public static readonly DependencyProperty ColumnsDependencyProperty =
        DependencyProperty.Register(nameof(Columns), typeof(int), typeof(GridEx), new PropertyMetadata(0));

    DependencyPropertyDescriptor ColumnsPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(ColumnsDependencyProperty, typeof(GridEx));

    public GridEx()
    {
        ColumnsPropertyDescriptor?.AddValueChanged(this, delegate
        {
            ColumnDefinitions.Clear();
            for (int i = 0; i < Columns; i++)
                ColumnDefinitions.Add(new ColumnDefinition());
        });
    }
于 2016-11-24T17:59:42.140 に答える
1

あなたが求めることを正確に行うヘルパーについては、こちらをご覧ください: https://rachel53461.wordpress.com/2011/09/17/wpf-grids-rowcolumn-count-properties/

上記のリンクからプロジェクトにヘルパーを追加した場合は、次のように使用できます。

<Grid local:GridHelpers.RowCount="6"
      local:GridHelpers.StarRows="5"
      local:GridHelpers.ColumnCount="4"
      local:GridHelpers.StarColumns="1,3">
</Grid>
于 2015-06-05T10:04:24.150 に答える