简体   繁体   中英

Add multiple Grids in Gridview using C#

Apologize if i am wrong, i am new to metro apps i need multiple grids fit into single grid view,This can be done using XAML by following code

<GridView x:Name="qw" Width="1052" Height="554" HorizontalAlignment="Left" Background="Black">
       <Grid Background="White" Height="284" Width="270"/>
       <Grid Background="White" Height="284" Width="270"/>
       <Grid Background="White" Height="284" Width="270"/>
       <Grid Background="White" Height="284" Width="270"/>
       <Grid Background="White" Height="284" Width="270"/>
</GridView>

But i want to do this in C#, Please help me. Thanks in advance

You can declare a data template (that has Grid), and bind ItemsSource to some collection property of ViewModel.

There will be as many grid, in GridView as many items in ViewModel Collection.

XAML code

        < GridView x:Name="qw" ItemsSource="{Binding Items}" Width="1052" Height="554" HorizontalAlignment="Left" Background="Black">
            < GridView.ItemTemplate>
                < DataTemplate>
                    < Grid Background="White" Height="284" Width="270"/>
                </DataTemplate>
            </GridView.ItemTemplate>
        </GridView>

View Model code

public ObservableCollection<String> Items { get; set; }
...
Items = new ObservableCollection<string>();
this.Items.Add("Item 1");
this.Items.Add("Item 1");
this.Items.Add("Item 1");
this.Items.Add("Item 1");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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