Add an image in a WPF button

前端 未结 7 938
执念已碎
执念已碎 2020-12-23 12:59

I tried this solution:

7条回答
  •  一向
    一向 (楼主)
    2020-12-23 13:51

    Please try the below XAML snippet:

    
    

    In XAML elements are in a tree structure. So you have to add the child control to its parent control. The below code snippet also works fine. Give a name for your XAML root grid as 'MainGrid'.

    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;
    MainGrid.Children.Add(btn);
    

提交回复
热议问题