【问题标题】:How do I generate WPF controls through code如何通过代码生成 WPF 控件
【发布时间】:2010-09-05 16:11:36
【问题描述】:

我试图了解 XAML,并认为我会尝试编写一些代码。

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

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#:

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

更新

这是一个令人毛骨悚然的地方:

  • 在 XP 上使用 VS2008 Pro

  • WPFbrowser 项目模板(3.5 验证)

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

【问题讨论】:

    标签: c# .net wpf xaml


    【解决方案1】:

    WPF 使用了一个叫做attached properties 的时髦东西。所以在你的 XAML 中你可以这样写:

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

    这将有效地将 TextBlock 移动到网格的单元格 (0,0) 中。

    在代码中这看起来有点奇怪。我相信它会是这样的:

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

    查看上面的链接 - 附加属性使 XAML 中的事情变得非常容易,也许以牺牲直观的代码为代价。

    【讨论】:

      【解决方案2】:

      单元格位置是一个附加属性 - 该值属于 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);
      

      【讨论】:

        【解决方案3】:

        使用 Grid 类的附加属性。

        在 C# 中:

        Grid.SetRow( cell, rownumber )

        在 XAML 中:

        &lt;TextBlock Grid.Row="1" /&gt;

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

        【讨论】:

          【解决方案4】:

          这是一些示例

          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
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-10-31
            • 1970-01-01
            • 1970-01-01
            • 2023-01-14
            • 1970-01-01
            相关资源
            最近更新 更多