简体   繁体   English

如何通过代码生成WPF控件

[英]How do I generate WPF controls through code

I was trying to get my head around XAML and thought that I would try writing some code. 我试图了解XAML,并认为我会尝试编写一些代码。

Trying to add a grid with 6 by 6 column definitions then add a text block into one of the grid cells. 尝试添加具有6乘6列定义的网格,然后将文本块添加到其中一个网格单元格中。 I don't seem to be able to reference the cell that I want. 我似乎无法引用我想要的细胞。 There is no method on the grid that I can add the text block too. 网格上没有方法可以添加文本块。 There is only grid.children.add(object), no Cell definition. 只有grid.children.add(对象),没有Cell定义。

XAML: XAML:

<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#: 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);
}

Update 更新

Here is the spooky bit: 这是幽灵般的一点:

  • Using VS2008 Pro on XP 在XP上使用VS2008 Pro

  • WPFbrowser Project Template (3.5 verified) WPFbrowser项目模板(3.5已验证)

I don't get the methods in autocomplete. 我没有自动完成方法。

WPF makes use of a funky thing called attached properties . WPF使用一种称为附加属性的时髦东西。 So in your XAML you might write this: 所以在你的XAML中你可以这样写:

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

And this will effectively move the TextBlock into cell (0,0) of your grid. 这将有效地将TextBlock移动到网格的单元格(0,0)中。

In code this looks a little strange. 在代码中,这看起来有点奇怪。 I believe it'd be something like: 我相信它会是这样的:

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

Have a look at that link above - attached properties make things really easy to do in XAML perhaps at the expense of intuitive-looking code. 看看上面的链接 - 附加的属性使XAML中的事情变得非常容易,可能会牺牲直观的代码。

The cell location is an attached property - the value belongs to the TextBlock rather than Grid. 单元格位置是附加属性 - 该值属于TextBlock而不是Grid。 However, since the property itself belongs to Grid, you need to use either the property definition field or the provided static functions. 但是,由于属性本身属于Grid,因此您需要使用属性定义字段或提供的静态函数。

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);

Use attached properties of the Grid class. 使用Grid类的附加属性。

in C#: 在C#中:

Grid.SetRow( cell, rownumber )

In XAML: 在XAML中:

<TextBlock Grid.Row="1" />

Also, I would advice if you do not use dynamic grids, use the XAML markup language. 另外,如果您不使用动态网格,我建议使用XAML标记语言。 I know, it has a learning curve, but once you mastered it, it is so much easier, especially if you are going to use ControlTemplates and DataTemplates! 我知道,它有一个学习曲线,但是一旦你掌握了它,它就会变得容易得多,特别是如果你要使用ControlTemplates和DataTemplates! ;) ;)

Here is some sample 这是一些样本

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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM