Как выводить данные в TextBlock через цикл в WPF
у меня есть программа и в этой программе есть массив и я хочу, чтобы значения массива соответственно выводились в соответствующие им TextBolck.
вот код
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace AirplaneRegistrationUI
{
/// <summary>
/// Логика взаимодействия для MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public int[] sectorsInAiplane = { 40, 35, 35, 50 };
public MainWindow()
{
InitializeComponent();
for (int i = 0; i < sectorsInAiplane.Length; i++)
{
firstSector.Text = Convert.ToString($"There are {sectorsInAiplane[i]} free seats left in sector {i}");
secondSector.Text = Convert.ToString($"There are {sectorsInAiplane[i]} free seats left in sector {i}");
thirdSector.Text = Convert.ToString($"There are {sectorsInAiplane[i]} free seats left in sector {i}");
fourthSector.Text = Convert.ToString($"There are {sectorsInAiplane[i]} free seats left in sector {i}");
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Environment.Exit(0);
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
sectorsInAiplane[0] -= 5;
}
}
}
вот XAML код
<Window x:Class="AirplaneRegistrationUI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:AirplaneRegistrationUI"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="900">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="45"/>
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150*"/>
<ColumnDefinition Width="155*"/>
<ColumnDefinition Width="143*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="1" Text="Airplane Registration." FontSize="30" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock Grid.Row="1" Grid.Column="0" Text="Select an operation." FontSize="24"/>
<TextBlock Grid.Row="2" Grid.Column="0" Text="1 - Booking seats." FontSize="24"/>
<TextBlock Grid.Row="3" Grid.Column="0" Text="2 - Cancel of booking seats." FontSize="24"/>
<TextBlock Grid.Row="4" Grid.Column="0" Text="3 - Quit the program." FontSize="24"/>
<TextBlock Grid.Row="1" Grid.Column="1" Text="Free Places" FontSize="33"/>
<TextBlock Grid.Row="1" Grid.Column="2" x:Name="firstSector"/>
<TextBlock Grid.Row="2" Grid.Column="2" x:Name="secondSector"/>
<TextBlock Grid.Row="3" Grid.Column="2" x:Name="thirdSector"/>
<TextBlock Grid.Row="4" Grid.Column="2" x:Name="fourthSector"/>
<Button Grid.Row="5" Grid.Column="0" Content="Booking seats." FontSize="27" Click="Button_Click_1"/>
<Button Grid.Row="5" Grid.Column="1" Content="Cancel of booking seats" FontSize="27"/>
<Button Grid.Row="5" Grid.Column="2" Content="Quit the program" FontSize="27" Click="Button_Click"/>
</Grid>
</Window>