C# 如何通过代码生成WPF控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-07-31 16:48:09  来源:igfitidea点击:

How do I generate WPF controls through code

提问by

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. 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.

尝试添加具有 6 x 6 列定义的网格,然后将文本块添加到其中一个网格单元格中。我似乎无法引用我想要的单元格。网格上没有方法可以添加文本块。只有 grid.children.add(object),没有 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

  • WPFbrowser Project Template (3.5 verified)

  • 在 XP 上使用 VS2008 Pro

  • WPFbrowser 项目模板(3.5 验证)

I don't get the methods in autocomplete.

我没有得到自动完成中的方法。

采纳答案by Matt Hamilton

WPF makes use of a funky thing called attached properties. So in your XAML you might write this:

WPF 使用了一种叫做附加属性的时髦东西。所以在你的 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 中的事情变得非常容易,这可能会牺牲直观的代码。

回答by Zooba

The cell location is an attached property - the value belongs to the TextBlock rather than Grid. However, since the property itself belongs to Grid, you need to use either the property definition field or the provided static functions.

单元格位置是一个附加属性 - 该值属于 TextBlock 而不是 Grid。但是,由于属性本身属于 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);

回答by Arcturus

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

另外,如果您不使用动态网格,我会建议您使用 XAML 标记语言。我知道,它有一个学习曲线,但是一旦你掌握了它,它就会容易得多,特别是如果你要使用 ControlTemplates 和 DataTemplates!;)

回答by Developer

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