简体   繁体   中英

How to count for each category in uwp

I have the following xaml code that generates the UI. You can see the screenshot. I would like to count the quantity for each plat let's say for my image plat 2 has 5qt and plat 1 has 3qt and plat 3 has 3qt.

这里是 x、y 和 z

Here is the code that I would like to change instead of x, y and z I would like to have the quantity for each plat.

<TextBlock Text="There are x plat1, y plat2 and z plat3"
           Style="{StaticResource BaseTextBlockStyle}" />

Here is my full xaml code

<Page x:Class="TakeOutUI.GestionCommandes"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:TakeOutUI"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      Background="#eee">

    <ScrollViewer>
        <Grid Margin="30,0, 30, 30">
            <StackPanel>
                <TextBlock Text="Commandes à préparer"
                           Style="{StaticResource Titre1}" />
                <StackPanel Orientation = "Horizontal">
                    <Button x:Name="sortBtn"  Content="Date Λ"  VerticalAlignment="Top" Click = "OnSortDateClick"/>
                    <CheckBox  x:Name="chkVoirTouts"  Content="Voir tout les commandes"  Click = "OnVoirTouts" />
                    <Button Content="+" HorizontalAlignment="Stretch" Click="Nav_AjoutCommande" />
                </StackPanel>
                <TextBlock Text="There are x plat1, y plat2 and z plat3"
                           Style="{StaticResource BaseTextBlockStyle}" />
                <ListView x:Name="ListCommandes">
                    <ListView.ItemContainerStyle>
                        <Style TargetType="ListViewItem">
                            <Setter Property="HorizontalContentAlignment"
                                    Value="Stretch" />
                            <Setter Property="VerticalAlignment"
                                    Value="Stretch" />
                            <Setter Property="HorizontalAlignment"
                                    Value="Stretch" />
                            <Setter Property="Margin"
                                    Value="0, 10, 0,0" />
                            <Setter Property="Background"
                                    Value="#fff" />
                        </Style>
                    </ListView.ItemContainerStyle>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <Grid Padding="5,15">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="60" />
                                </Grid.ColumnDefinitions>

                                <StackPanel  Grid.Column="0">
                                    <TextBlock Text="{Binding NomCommande}"
                                               Style="{StaticResource Titre3}" />
                                    <TextBlock>
                                        <Run Text="Date :"
                                             FontWeight="Bold" />
                                        <Run Text="{Binding DateCommande}" />
                                    </TextBlock>
                                </StackPanel>

                                <StackPanel  Grid.Column="1">
                                    <TextBlock Text="Plats à préparer :"
                                               Style="{StaticResource Titre3}" />

                                    <ListView ItemsSource="{Binding ItemsCommande}">
                                        <ListView.ItemContainerStyle>
                                            <Style TargetType="ListViewItem">
                                                <Setter Property="HorizontalContentAlignment"
                                                        Value="Stretch" />
                                            </Style>
                                        </ListView.ItemContainerStyle>
                                        <ListView.ItemTemplate>
                                            <DataTemplate>
                                                <Grid>
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="40" />
                                                        <ColumnDefinition Width="60" />
                                                        <ColumnDefinition Width="*" />
                                                    </Grid.ColumnDefinitions>
                                                    <FontIcon Grid.Column="0" FontFamily="Segoe MDL2 Assets"
                                                              Glyph="&#xF127;" />
                                                    <TextBlock Grid.Column="1">
                                                        <Run Text="Qté :"
                                                             FontWeight="Bold" />
                                                        <Run Text="{Binding QuantitePlat}" />
                                                    </TextBlock>
                                                    <TextBlock Grid.Column="2">
                                                        <Run Text="Plat :"
                                                             FontWeight="Bold" />
                                                        <Run Text="{Binding PlatId}" />
                                                    </TextBlock>
                                                </Grid>
                                            </DataTemplate>
                                        </ListView.ItemTemplate>
                                    </ListView>
                                </StackPanel>
                            </Grid>

                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>


            </StackPanel>
        </Grid>
    </ScrollViewer>
</Page>

Here are my 3 models

Commande

public class Commande
{
    [Key]
    public int IdCommande { get; set; }

    public Commande()
    {
        ItemsCommande = new Collection<ItemCommande>();
    }

    public string NomCommande { get; set; }
    public DateTime DateCommande { get; set; }
    public ICollection<ItemCommande> ItemsCommande { get; set; }

}

ItemCommande

public class ItemCommande
{
    [Key]
    public int IdDetailCommande { get; set; }
    public int QuantitePlat { get; set; }

    [ForeignKey("Commande")]
    public int CommandeId { get; set; }
    public Commande Commande { get; set; }

    [ForeignKey("Plat")]
    public int PlatId { get; set; }
    public Plat Plat {
        get; 
        set; 
    }
}

and Plat

public class Plat
{
    [Key]
    public int IdPlat { get; set; }
    public string NomPlat { get; set; }
}

And here is my context

public class Contexte : DbContext
{
    private static bool _created = false;
    public Contexte()
    {
        if (!_created)
        {
            _created = true;
            Database.EnsureDeleted();
            Database.EnsureCreated();
            InitialisateurBD.Seed();
        }
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Plat>();
        modelBuilder.Entity<Commande>();
        modelBuilder.Entity<ItemCommande>();
        base.OnModelCreating(modelBuilder);
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionbuilder)
    {
        optionbuilder.UseSqlite(@"Data Source=TakeOut.db");
    }

    public DbSet<Plat> Plats { get; set; }
    public DbSet<Commande> Commandes { get; set; }
    public DbSet<ItemCommande> ItemsCommandes { get; set; }

}

Thank you for your help.

You could check the following code to count the quantity for each plat:

int[] results = new int[4];
foreach(var cur in ViewModel)
{
    foreach(var item in cur.ItemsCommande)
    {
        results[item.PlatId] = results[item.PlatId] + item.QuantitePlat;
    }
}

var resultStr = "There are " + results.ElementAt(1).ToString() + " plat1, " + results.ElementAt(2).ToString() + " plat2 and " +
        results.ElementAt(3).ToString() + " plat3";
resultString.Text = resultStr;  // resultString is the TextBlock control

The ViewModel is used to replace your Contexte which contains the data for testing. You need to change the ViewModel to adapt your code.

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