我有以下生成用户界面的xaml代码。你可以看到屏幕截图。我想计算每个平台的数量,假设我的图片平台2为5qt,平台1为3qt,平台3为3qt。
这是我想要更改的代码,而不是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; }
}
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;
}
}
和平台
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
来适应你的代码。