wpf 后台动态添加控件(多个)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37033834/article/details/78538870

这是前台布局代码

    <StackPanel Margin="0,50,0,0" Orientation="Horizontal"  HorizontalAlignment="Center">
        <StackPanel>
            <Button Content="动态添加多个checkbox控件" Height="20" Padding="15,1,15,2" x:Name="btnAdd" Click="btnAdd_Click"></Button>
        </StackPanel>

    </StackPanel>

    <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto" Height="150">
        <Border Background="#BCBCBC" BorderBrush="#797979" BorderThickness="1"  Margin="5,0,5,5">
            <WrapPanel x:Name="addCheckbox"/>
        </Border>
    </ScrollViewer>

这是后台添加控件代码

 private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
        int num = 6;
        CheckBox[] check = new CheckBox[num];
        Thickness th = new Thickness();
        th.Bottom = 10;
        th.Left = 10;
        th.Right = 10;
        th.Top = 10;
        for (int i = 0; i < check.Length; i++)
        {
            check[i] = new CheckBox();
            //设置checkbox属性
            check[i].Margin = th;
            check[i].Content = i + 1;

            this.addCheckbox.Children.Add(check[i]);

        }
    }

猜你喜欢

转载自blog.csdn.net/qq_37033834/article/details/78538870
今日推荐