c# - 如何在 C# 代码后面的按钮中添加 StackPanel

标签 c# wpf xaml button

如何使用后面的 C# 代码在 Button 中添加 StackPanel(即将以下 XAML 转换为 C#)?没有 Button.Children.Add...

<Button>
   <StackPanel Orientation="Horizontal" Margin="10">
      <Image Source="foo.png"/>
   </StackPanel>
</Button>

最佳答案

  Image img = new Image();
  img.Source = new BitmapImage(new Uri("foo.png"));

  StackPanel stackPnl = new StackPanel();
  stackPnl.Orientation = Orientation.Horizontal;
  stackPnl.Margin = new Thickness(10);
  stackPnl.Children.Add(img);

  Button btn = new Button();
  btn.Content = stackPnl;

关于c# - 如何在 C# 代码后面的按钮中添加 StackPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5390270/

相关文章:

c# - WPF 数据网格在 c# 中与远程/在线 mysql 绑定(bind)

c# - 使用 DoubleAnimation 设置图像不透明度

c# - "The best overloaded Add method ' System.Collections.Generic.List.Add(System.Guid) ' for the collection initializer has some invalid arguments"

c# - MonoGame 应用程序说缺少 SDL.dll,即使它存在。为什么?

c# - 从 C# 中的另一个线程修改类属性

c# - 在代码中设置排序时,ColumnHeader 的箭头不指向任何方向

C# DataTable 更新多行

c# - 这个。关闭();在窗口 wpf 中不起作用

xaml - Xamarin.Forms:如何正确使用<OnPlatform>来选择要在TableSection中使用的控件?

c# - WP7 ListBox 选中的项目没有改变颜色