Paul-Henri Andrieu
Paul-Henri Andrieu

Reputation: 13

How to add data to a gridview in c# Windows UWP

I recently began with C# and I have the following problem : I have a class called "Classe", which provides me an object : Classe(String name) and another one called LesClasses which is actually an ObservableCollection. With this, I want to add all the "Classe" elements in a GridView in UI.

The main C# code :

namespace TestProject
{

    public sealed partial class MainPage : Windows.UI.Xaml.Controls.Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            LesClasses listeClasse = new LesClasses();
            Classe maClasse = new Classe("Test");
            listeClasse.Add(maClasse);
        }

        private void cd_Click(object sender, RoutedEventArgs e)
        {
            Classe classe = new Classe("test2");
            LesClasses listeClasse = new LesClasses();
            listeClasse.Add(classe);
        }
    }

    public class Classe
    {
        public String Nom { get; set; }

        public Classe(String nom)
        {
            this.Nom = nom;
        }
    }

    public class LesClasses : ObservableCollection<Classe>
    {
        public LesClasses()
        {

        }
    }

    public class DataTemplate : FrameworkTemplate{}
}

And the XAML :

<Page
    x:Class="TestProject.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:src="using:TestProject"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>

        <Grid.Resources>
            <src:LesClasses x:Key="classes"/>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="3*"/>
            <RowDefinition Height="0.5*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="20*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <TextBlock Text="MyClassroom" Grid.Column="1" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Segoe UI" FontWeight="SemiLight" FontSize="46"/>
        <GridView ItemsSource="{StaticResource classes}" Margin="10,10,10,10"  Grid.Column="1" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" FlowDirection="LeftToRight" Height="Auto" Width="Auto">
            <GridView.ItemTemplate>
                <DataTemplate>
                    <Button Margin="10,10,10,10" Height="Auto" Width="Auto" HorizontalAlignment="Center" VerticalAlignment="Center">
                        <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Orientation="Horizontal">
                            <SymbolIcon Symbol="OtherUser" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10,10,10,10"/>
                            <StackPanel>
                                <TextBlock Text="{Binding Nom}" FontFamily="Segoe UI" FontWeight="SemiLight" FontSize="26"/>
                            </StackPanel>
                        </StackPanel>
                    </Button>
                </DataTemplate>
            </GridView.ItemTemplate>
            <GridView.ItemsPanel>
                <ItemsPanelTemplate>
                    <ItemsWrapGrid MaximumRowsOrColumns="3"/>
                </ItemsPanelTemplate>
            </GridView.ItemsPanel>

            <GridView.GroupStyle>
                <GroupStyle HidesIfEmpty="True">
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <Grid Background="LightGray" Margin="0">
                                <TextBlock Text='{Binding Name}' 
                                   Foreground="Black" Margin="30"
                                   Style="{StaticResource HeaderTextBlockStyle}"/>
                            </Grid>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>

                </GroupStyle>
            </GridView.GroupStyle>
        </GridView>
        <Button Name="cd" Click="cd_Click" Grid.Column="1" Grid.Row="2" Content="Ajouter la classe test2" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" FontFamily="Segoe UI" FontSize="18" FontWeight="SemiLight"/>
    </Grid>
</Page>

When I launch the app, and I click on the button, nothing happens. Can anyone help me please?

Upvotes: 0

Views: 304

Answers (1)

Mac
Mac

Reputation: 959

In simplest way here's how you can solve your problem, in your code-behind you need to do the following:

  1. Add a property for the observable collection:

    public LesClasses Classes { get; set; }
    
  2. Initialize the list:

    Classes = new LesClasses();
    Classe maClasse = new Classe("Test");
    Classes.Add(maClasse);
    
  3. Assign MainPage.cs as your DataContext

    DataContext = this;
    
  4. In your event you need to add the new instance of Classe to your observable collection property.

    private void cd_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
        Classe classe = new Classe("test2");
        Classes.Add(classe);
    }
    

In your xaml, change the ItemSource to bind to your collection property:

<GridView ItemsSource="{Binding Classes}"

Your MainPage class should look like this:

public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            Classes = new LesClasses();
            Classe maClasse = new Classe("Test");
            Classes.Add(maClasse);
            DataContext = this;
        }

        public LesClasses Classes { get; set; }

        private void cd_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            Classe classe = new Classe("test2");
            Classes.Add(classe);
        }
    }

Output:

enter image description here

Keep studying C# and UWP/WPF.

Upvotes: 1

Related Questions