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.Contentinstead of usingButton.Children.AddAs a longer explanation:
Content.Children– e.g. StackPanel, Grid, WrapPanel, Canvas, etc.As your code already shows, you can set the
Contentof 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.