どんな生成コンポーネントのラインナップを通した管理コード

StackOverflow https://stackoverflow.com/questions/7586

  •  08-06-2019
  •  | 
  •  

質問

いることができるように私の頭の周りーと思う書面の一部のコードです。

追加しようとしているグリッド6 6個のカラム定義を追加してテキストブロックのグリッド細胞いることもできるでしょう参考に細胞がってきた。メソッドはありませんのグリッドにて追加する事ができますテキストブロックです。ある。子どもたち。add(object)は、細胞の定義で設定します。

ー:

<Page x:Class="WPF_Tester.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Page1"
    Loaded="Page_Loaded">

</Page>

C#:

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    //create the structure
    Grid g = new Grid();
    g.ShowGridLines = true;
    g.Visibility = Visibility.Visible;

    //add columns
    for (int i = 0; i < 6; ++i)
    {
        ColumnDefinition cd = new ColumnDefinition();
        cd.Name = "Column" + i.ToString();

        g.ColumnDefinitions.Add(cd);
    }
    //add rows
    for (int i = 0; i < 6; ++i)
    {
        RowDefinition rd = new RowDefinition();
        rd.Name = "Row" + i.ToString();

        g.RowDefinitions.Add(rd);
    }
    TextBlock tb = new TextBlock();
    tb.Text = "Hello World";

    g.Children.Add(tb);
}

更新

この不気味なビット

  • 使用VS2008XP Pro

  • WPFbrowserプロジェクトテンプレート(3.5確認)

得られませんのメソッドをオートコンプリート.

役に立ちましたか?

解決

コンポーネントのラインナップするのに使用しのファンキーというも 属性.うお話サイトの最新トレンドが書けます:

<TextBlock Grid.Row="0" Grid.Column="0" />

この効果的に移動させTextBlock細胞(0,0)のグリッド

コードをこのう少し不思議に思います。そうするようなもの:

g.Children.Add(tb);
Grid.SetRow(tb, 0);
Grid.SetColumn(tb, 0);

している上記リンクを貼付した物件のことのないようにくいポうに直感的に見ます。

他のヒント

の細胞の位置が付属性の値に所属するTextBlockよりグリッドしかしながら、物件そのものの所属するグリッドを使用する必要がありいずれかの定義は、静的ます。

TextBlock tb = new TextBlock();
//
// Locate tb in the second row, third column.
// Row and column indices are zero-indexed, so this
// equates to row 1, column 2.
//
Grid.SetRow(tb, 1);
Grid.SetColumn(tb, 2);

取り付けて使用特性のグリッドのクラスです。

クライアントまで、フルのC#:

Grid.SetRow( cell, rownumber )

にー:

<TextBlock Grid.Row="1" />

また、私の助言を使用しない場合の動的なグリッド、プ言語です。ないので学習曲線はまさしくその通りなのだが、マスタではなく、特にご利用ControlTemplatesとDataTemplates!;)

ここではサンプル

Grid grid = new Grid();

// Set the column and row definitions
grid.ColumnDefinitions.Add(new ColumnDefinition() {
     Width = new GridLength(1, GridUnitType.Auto) });
grid.ColumnDefinitions.Add(new ColumnDefinition() {
     Width = new GridLength(1, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition() {
     Height = new GridLength(1, GridUnitType.Auto) });
grid.RowDefinitions.Add(new RowDefinition() {
     Height = new GridLength(1, GridUnitType.Auto) });

// Row 0
TextBlock tbFirstNameLabel = new TextBlock() { Text = "First Name: "};
TextBlock tbFirstName = new TextBlock() { Text = "John"};

grid.Children.Add(tbFirstNameLabel ); // Add to the grid
Grid.SetRow(tbFirstNameLabel , 0); // Specify row for previous grid addition
Grid.SetColumn(tbFirstNameLabel , 0); // Specity column for previous grid addition

grid.Children.Add(tbFirstName ); // Add to the grid
Grid.SetRow(tbFirstName , 0);  // Specify row for previous grid addition
Grid.SetColumn(tbFirstName , 1); // Specity column for previous grid addition

// Row 1
TextBlock tbLastNameLabel = new TextBlock() { Text = "Last Name: "};
TextBlock tbLastName = new TextBlock() { Text = "Smith"};

grid.Children.Add(tbLastNameLabel ); // Add to the grid
Grid.SetRow(tbLastNameLabel , 1);  // Specify row for previous grid addition
Grid.SetColumn(tbLastNameLabel , 0); // Specity column for previous grid addition

grid.Children.Add(tbLastName ); // Add to the grid
Grid.SetRow(tbLastName , 1);  // Specify row for previous grid addition
Grid.SetColumn(tbLastName , 1); // Specity column for previous grid addition
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top