2

I have big button (Gray border),where I have to add an image using c# and also I have to add text under the image also using c# (there is also red button inside the button) ,I really do not understand how to add image in c# code (and text) and how to call it. It have to be something like button.content=image... I found some information, but I do not understand what to write in c#?

my xaml:

  <Style x:Key="ButtonStyle"  TargetType="{x:Type Button}">
        <Setter Property="Foreground" Value="Black"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="FontSize" Value="20px" />
        <Setter Property="ToolTipService.IsEnabled" Value="False"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border BorderBrush="#E6E6E6" BorderThickness="1">
                        <Grid x:Name="btnGrid" Width="320px" Height="320px">

                            <Image  Grid.Row="0"
                                  Stretch="Fill"
                                  HorizontalAlignment="Center"
                                  Width="131" 
                                  Height="98"
                                  Margin="0,69px,0,0"
                                  Source="{TemplateBinding Content}" />
                            <TextBlock  x:Name="btnText" Grid.Row="0"
                                      Text="{TemplateBinding ToolTip}"
                                      HorizontalAlignment="Center" 
                                      Margin="0,207,0,0"
                                      TextAlignment="Center"
                                      TextWrapping="Wrap"
                                      FontFamily="{TemplateBinding FontFamily}"
                                      FontSize="{TemplateBinding FontSize}"
                                      Foreground="{TemplateBinding Foreground}" 
                                      FontStretch="{TemplateBinding FontStretch}" />
                            <Button  x:Name="btnButton" Grid.Row="2" Style="{DynamicResource MetroButtonStyle}" 
                                      ToolTip="{TemplateBinding ToolTip}"
                                      HorizontalAlignment="Center" 
                                      VerticalAlignment="Center" 
                                      Background="#F86C4D"
                                      Margin="0,238px,0,0"
                               Height="50"
                               Width="150"
                                      Foreground="White"
                                      FontFamily="{TemplateBinding FontFamily}"

                                       />
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

My cs:

 namespace _3_Buttons
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }     
    }
}
LTU
  • 195
  • 1
  • 3
  • 18

1 Answers1

2

I have done this : xaml file:

<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button x:Name="button1" HorizontalAlignment="Left" Margin="127,69,0,0" VerticalAlignment="Top" Width="215" Height="110"/>

    </Grid>
</Window>

xaml.cs file

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace WpfApplication5
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Image img = new Image();
            string path = System.AppDomain.CurrentDomain.BaseDirectory + "chart2 - Copy.png";
            img.Source = new BitmapImage(new Uri(path));

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

            button1.Content = stackPnl;
        }
    }
}
DeshDeep Singh
  • 1,817
  • 2
  • 23
  • 43
  • Image appear if I write how you, but if I write : image disappear, its possible add image and use style which I wrote? (Style code is in xaml code) – LTU Jul 28 '15 at 12:28
  • No since we have created a stack panel in code behind to add an image or probable controls you want you have to do this way, since you wanted programmatically but your approach will be totally different from this one. – DeshDeep Singh Jul 28 '15 at 12:29