简体   繁体   English

如何对 uwp 中的每个类别进行计数

[英]How to count for each category in uwp

I have the following xaml code that generates the UI.我有以下生成 UI 的 xaml 代码。 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.我想计算每个平台的数量,假设我的图像平台 2 有 5 夸脱,平台 1 有 3 夸脱,平台 3 有 3 夸脱。

这里是 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.这是我想要更改的代码,而不是 x、y 和 z 我想要每个平台的数量。

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

Here is my full xaml code这是我完整的 xaml 代码

<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这是我的 3 个模型

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. ViewModel用于替换包含测试数据的Contexte You need to change the ViewModel to adapt your code.您需要更改ViewModel以适应您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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