在运行时将按钮添加到所需的堆栈面板。 [英] Add button to the desired stackpanel at run time.

查看:78
本文介绍了在运行时将按钮添加到所需的堆栈面板。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初我有xaml代码。

Originally I have the xaml code.

<Window x:Class="Test.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"

        mc:Ignorable="d"

        Height="802" Width="883">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="702"></ColumnDefinition>
            <ColumnDefinition Width="150"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Viewbox Grid.Column="0">
            <StackPanel Orientation="Vertical">
                <StackPanel Orientation="Horizontal">
                    <Label FontSize="48" Margin="57,0,20,0" >A</Label>
                    <Label FontSize="48" Margin="18,0,20,0" >B</Label>
                    <Label FontSize="48" Margin="18,0,20,0" >C</Label>
                    <Label FontSize="48" Margin="18,0,20,0" >D</Label>
                    <Label FontSize="48" Margin="18,0,20,0" >E</Label>
                    <Label FontSize="48" Margin="18,0,20,0" >F</Label>
                    <Label FontSize="48" Margin="18,0,20,0" >G</Label>
                    <Label FontSize="48" Margin="18,0,20,0" >H</Label>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Label FontSize="48" Margin="0,0,7,0">8</Label>
                    <Border Style="{DynamicResource ChessL}">
                        <Button x:Name="A8" Height="Auto" Background="{x:Null}" FontSize="40" Foreground="Black"  KeyDown="OnKeyDown" Tag="{Binding A8Color,Mode=TwoWay}"></Button>
                    </Border>
                    <Border Style="{DynamicResource Chess}">
                        <Button x:Name="B8" Height="Auto" Background="{x:Null}" FontSize="40" Foreground="Black"  KeyDown="OnKeyDown" Tag="{Binding B8Color,Mode=TwoWay}"></Button>
                    </Border>



这只是一个按钮,我有很多类似的按钮需要添加。现在我想以编程方式添加按钮。



困难的是如何在代码中处理Viewbox,StackPanel,Label,border和style。



我尝试了什么:



我知道在代码中添加一个简单的按钮:


This is just one button, I have a lot of similar buttons to be added. Now I want to add the buttons programmatically.

The difficult thing is how to deal with Viewbox, StackPanel, Label, border and style in code.

What I have tried:

I know to add a simple button by the code:

Button btn = new Button();



但是如何添加Viewbox,边框和样式


But how to add Viewbox, borders and style

推荐答案

Google [ ^ ]返回218 000结果(0.47秒),即:

c# - WPF:如何在动态创建的WPF窗口中动态添加控件 - 堆栈溢出 [ ^ ]

c# - WPF:从代码中添加控件 - Stack Overflow [ ^ ]

动态 - 以编程方式将控件添加到WPF表单 - 堆栈溢出 [ ^ ]
Google[^] returns about 218 000 results (in 0.47 s), i.e.:
c# - WPF: How to dynamically Add Controls in dynamically created WPF Window - Stack Overflow[^]
c# - WPF: Add controls from code - Stack Overflow[^]
dynamic - Programmatically Add Controls to WPF Form - Stack Overflow[^]


这篇关于在运行时将按钮添加到所需的堆栈面板。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆