How to Add an image in a WPF button ?
How to Add an image in a WPF button ?
Use this content templete:
<Button Grid.Row="2" Grid.Column="0" Width="20" Height="20" Template="{StaticResource SomeTemplate}"> <Button.ContentTemplate> <DataTemplate> <Image Source="../Folder1/Img1.png" Width="20" /> </DataTemplate> </Button.ContentTemplate> </Button>
Try this code for the solution:
<Button Height="100" Width="100"> <StackPanel> <Image Source="img.jpg" /> <TextBlock Text="Blabla" /> </StackPanel> </Button>
Use this xaml code:
<Button Width="300" Height="50"> <StackPanel Orientation="Horizontal"> <Image Source="Pictures/img.jpg" Width="20" Height="20"/> <TextBlock Text="Blablabla" VerticalAlignment="Center" /> </StackPanel> </Button>
If XAML components are in a tree structure, Add the control to its parent control. The beneath code piece additionally works fine. Give a name for your XAML root framework 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);