[英]How to count for each category in uwp
我有以下生成 UI 的 xaml 代碼。 你可以看到截圖。 我想計算每個平台的數量,假設我的圖像平台 2 有 5 誇脫,平台 1 有 3 誇脫,平台 3 有 3 誇脫。
這是我想要更改的代碼,而不是 x、y 和 z 我想要每個平台的數量。
<TextBlock Text="There are x plat1, y plat2 and z plat3"
Style="{StaticResource BaseTextBlockStyle}" />
這是我完整的 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="" />
<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>
這是我的 3 個模型
指揮部
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; }
}
物品命令
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;
}
}
和平台
public class Plat
{
[Key]
public int IdPlat { get; set; }
public string NomPlat { get; set; }
}
這是我的背景
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; }
}
謝謝您的幫助。
您可以檢查以下代碼來計算每個平台的數量:
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
ViewModel
用於替換包含測試數據的Contexte
。 您需要更改ViewModel
以適應您的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.