ในรูปแบบที่ง่ายที่สุด...
ฉันต้องการสร้าง StackPanels ได้มากเท่าที่ต้องการ จากนั้นจึงเพิ่มสี่เหลี่ยมผืนผ้าลงไป จากนั้นเพื่อให้สามารถเปลี่ยนสีเติมของสี่เหลี่ยมอันใดอันหนึ่งได้เมื่อฉันคลิกปุ่มเริ่มเป็นต้น ทั้งหมดในโค้ดเบื้องหลัง
ความช่วยเหลือใด ๆ ที่จะได้รับการชื่นชม
ตัวอย่างเช่น หากเบียร์ที่เราชื่นชอบเป็นผู้เขียนกรอบงาน ฉันสามารถทำได้ดังนี้:
เอ็กซ์เอเอ็มแอล:
<Page
x:Class="Test2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Test2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Button Name="StartButton" Content="Start" Click="StartButton_Click" Height="30" Width="200" Margin="5"/>
</StackPanel>
<StackPanel Grid.Row="1" Name="myStackPanel" VerticalAlignment="Top"/>
</Grid>
</Page>
รหัสเบื้องหลัง:
namespace Test2
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
for (var i = 0; i < 5; i++) // The 5 here could be any number
{
myStackPanel.Children.Add(new StackPanel
{
Name = "myPanel" + i,
Orientation = Orientation.Horizontal
});
for (var j = 0; j < 10; j++) // The 10 here could be any number
{
("myPanel" + i).Children.Add(new Rectangle
{
Name = "myRectangle" + i + "-" + j,
Fill = new SolidColorBrush(Colors.Black),
Width = 20,
Height = 20,
Margin = new Thickness(1)
});
}
}
}
private void StartButton_Click(object sender, RoutedEventArgs e)
{
// E.G. To change the Fill color of Rectangle4 in StackPanel2
("myRectangle" + 2 + "-" + 4).Fill = new SolidColorBrush(Colors.Red);
}
}
}