Search code examples
c#windowswindows-store-apps

C# Windows APP: Adding UIElements to Grid/GridView


im coding a Windows 8 appliation with C#, but im stuck since friday with some UI adjustment. I have a GridView and i want to add UIElements on the GridView, so no problem:

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

Thats how i add them, but im reading them out of an XML and the XML defines also the position of the Button (Row/Col). So my Question is how do i add these Buttons on the specific Row/Column?

Here is how my GridView look like, i didnt changed yet anything (XAML):

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

Solution

  • Ive found a way but forgot to update this: If you want to to set a Button for example in a specific row/col then you have to do this:

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

    You have to be sure that the Row/Column also exists so you can use:

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

    Of course you do that all before you add the UIElement to the Grid!