如何在 C# 代码后面的 Button 中添加 StackPanel

发布于 2024-10-24 18:49:23 字数 290 浏览 1 评论 0原文

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

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤城病女 2024-10-31 18:49:23
  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;
  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;
写给空气的情书 2024-10-31 18:49:23

设置 Button.Content 而不是使用 Button.Children.Add

作为更长的解释:

  • Button 是一个“只有 1 个子级”的控件 - 它的 Content.
  • 只有极少数控件(通常是“面板”)可以包含零个或多个 Children 列表 - 例如 StackPanel、Grid、WrapPanel、Canvas 等。

正如您的代码所示,您可以设置 Content 按钮成为面板 - 这将允许您添加多个子控件。但是,实际上在您的示例中,不需要 StackPanel 和 Image。看起来您的 StackPanel 只添加了 Padding - 如果您愿意,您可以将 Padding 添加到 Image 而不是 StackPanel 中。

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.

阿楠 2024-10-31 18:49:23

使用

<Window.Resources>   
    <ImageSource x:Key="LeftMenuBackgroundImage">index.jpg</ImageSource>
    <ImageBrush x:Key="LeftMenuBackgroundImageBrush" 
     ImageSource="{DynamicResource LeftMenuBackgroundImage}"/> 
</Window.Resources>

像这样在代码隐藏中

Button btn = new Button();
        btn.HorizontalContentAlignment = HorizontalAlignment.Stretch;
        btn.VerticalContentAlignment = VerticalAlignment.Stretch;
        StackPanel stk = new StackPanel();
        stk.Orientation = Orientation.Horizontal;
        stk.Margin = new Thickness(10, 10, 10, 10);
        stk.SetResourceReference(StackPanel.BackgroundProperty, "LeftMenuBackgroundImageBrush");
        btn.Content = stk;

Use like this

<Window.Resources>   
    <ImageSource x:Key="LeftMenuBackgroundImage">index.jpg</ImageSource>
    <ImageBrush x:Key="LeftMenuBackgroundImageBrush" 
     ImageSource="{DynamicResource LeftMenuBackgroundImage}"/> 
</Window.Resources>

and in Codebehind

Button btn = new Button();
        btn.HorizontalContentAlignment = HorizontalAlignment.Stretch;
        btn.VerticalContentAlignment = VerticalAlignment.Stretch;
        StackPanel stk = new StackPanel();
        stk.Orientation = Orientation.Horizontal;
        stk.Margin = new Thickness(10, 10, 10, 10);
        stk.SetResourceReference(StackPanel.BackgroundProperty, "LeftMenuBackgroundImageBrush");
        btn.Content = stk;
千里故人稀 2024-10-31 18:49:23

在 Xaml 中:

<Button x:Name="Btn" Click="Btn_Click" Orientation="Horizontal" Margin="10">
  <StackPanel>
     <Image Source="foo.png" Height="16" Width="16"/>
  </StackPanel>
</Button>

在 C# 中:

Button btn = new Button();
StackPanel panel = new StackPanel();
Image img = new Image
{
   Source = "../foo.png"
}
panel.Children.Add(img);
btn.Content = panel;

我建议您将图像放在 xaml 资源中:

<Window.Resources>
    <BitmapImage x:Key="Img" UriSource="/Img/foo.png"/>
</Window.Resources>

并像这样调用它:

Image img = new Image
{
   Source = (BitmapImage)FindResource("Img")
};

In Xaml :

<Button x:Name="Btn" Click="Btn_Click" Orientation="Horizontal" Margin="10">
  <StackPanel>
     <Image Source="foo.png" Height="16" Width="16"/>
  </StackPanel>
</Button>

In C# :

Button btn = new Button();
StackPanel panel = new StackPanel();
Image img = new Image
{
   Source = "../foo.png"
}
panel.Children.Add(img);
btn.Content = panel;

I advise you to put the image in xaml resources :

<Window.Resources>
    <BitmapImage x:Key="Img" UriSource="/Img/foo.png"/>
</Window.Resources>

And call it like this :

Image img = new Image
{
   Source = (BitmapImage)FindResource("Img")
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文