如何在C#code添加的StackPanel在按钮后面 [英] How to add a StackPanel in a Button in C# code behind
问题描述
如何添加的StackPanel背后使用C#code 的按钮(即转换下面的XAML代码为C#)?没有 Button.Children.Add
...
<按钮和GT;
< StackPanel的方向=横向保证金=10>
<图像源=foo.png/>
< / StackPanel的>
< /按钮>
设置 Button.Content
而不是使用 Button.Children.Add
至于更详细的解释:
- 按钮是唯一有1个小孩的控制 - 它的
内容
。 - 只有很少的控制(一般是面板),可以包含零个或多个列表
孩子
- 例如StackPanel中,网格,WrapPanel,帆布等。
随着你的code已经具备了,你可以设置一个按钮内容
是一个面板 - 这将EHN允许你再添加多个子控件。然而,真正你的榜样,那么就没有必要拥有的StackPanel以及图像。好像你的StackPanel只添加填充 - 你可以,如果你想将填充添加到图像而不是对的StackPanel
How to add a StackPanel in a Button using c# code behind (i.e. convert the following XAML to C# )? There is no Button.Children.Add
...
<Button>
<StackPanel Orientation="Horizontal" Margin="10">
<Image Source="foo.png"/>
</StackPanel>
</Button>
Set Button.Content
instead of using Button.Children.Add
As a longer explanation:
- Button is a control which "only has 1 child" - its
Content
. - Only very few controls (generally "Panels") can contain a list of zero or more
Children
- e.g. StackPanel, Grid, WrapPanel, Canvas, etc.
As your code already shows, you can set the Content
of a Button to be a Panel - this would ehn allow you to then add multiple child controls. However, really in your example, then there is no need to have the StackPanel as well as the Image. It seems like your StackPanel is only adding Padding - and you could add the Padding to the Image rather than to the StackPanel if you wanted to.
这篇关于如何在C#code添加的StackPanel在按钮后面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!