小编gaw*_*gaw的帖子

动态创建 StackPanel 并在后面的代码中引用其中的任何一个

以最简单的形式...

我想创建尽可能多的 StackPanel,然后在其中添加矩形。然后,例如,当我单击“开始”按钮时,能够更改任何一个矩形的填充颜色。一切都在代码隐藏中。

任何帮助,将不胜感激。

例如,如果我们最喜欢的啤酒编写了框架,我可以这样做:

XAML:

<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>
Run Code Online (Sandbox Code Playgroud)

背后代码:

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" …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml uwp

3
推荐指数
1
解决办法
9558
查看次数

标签 统计

c# ×1

uwp ×1

wpf ×1

xaml ×1