【问题标题】:C# Windows APP: Adding UIElements to Grid/GridViewC# Windows APP:将 UIElements 添加到 Grid/GridView
【发布时间】:2015-07-05 16:07:15
【问题描述】:

我用 C# 编写了一个 Windows 8 应用程序,但我从星期五开始就卡住了,进行了一些 UI 调整。 我有一个 GridView,我想在 GridView 上添加 UIElements,所以没问题:

foreach(TouchpadButton btn in m_ButtonList)
{  
   mainGrid.Items.Add(btn);
}

这就是我添加它们的方式,但我从 XML 中读取它们,并且 XML 还定义了按钮的位置(行/列)。 所以我的问题是如何在特定的行/列上添加这些按钮?

这是我的 GridView 的样子,我还没有改变任何东西(XAML):

 <GridView HorizontalAlignment="Left" Margin="443,327,0,0" Grid.Row="1" VerticalAlignment="Top" Width="398" Height="223"/>

【问题讨论】:

    标签: c# windows windows-store-apps


    【解决方案1】:

    我找到了一种方法,但忘记更新了: 如果你想在特定的行/列中设置一个按钮,那么你必须这样做:

    Button btn = new Button(); // Empty Button
    int Row = 0;
    int Col = 1;
    Grid.SetRow(btn,Row);
    Grid.SetColumn(btn,Col);
    

    您必须确保行/列也存在,以便您可以使用:

    if(mainGrid.RowDefinitions.ElementAt(Row) != null && mainGrid.ColumnDefinitions.ElemantAt(Col) != null)
    {
    
     Grid.SetRow(btn,Row);
     Grid.SetColumn(btn,Col);
    }
    

    当然,在将 UIElement 添加到 Grid 之前,您都会这样做!

    【讨论】:

      猜你喜欢
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-12
      • 2012-08-09
      • 2010-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多