Nous allons utiliser un fichier contenant une image qui sera affichée sur le bouton de notre application.
Une fois que le fichier est importé dans Visual Studio il faut positionner son
Build Action à
Resource.
L'exemple devient alors :
public partial class Window7 : Window
{
Button button1 = new Button();
Button button2 = new Button();
StackPanel imgContent = new StackPanel();
string content = "Hello";
DockPanel dockPanel = new DockPanel();
SolidColorBrush scb = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0));
public Window7()
{
InitializeComponent();
dockPanel.Background = Brushes.Red;
dockPanel.LastChildFill = false;
this.AddChild(dockPanel);
Label lbl = new Label();
lbl.Content = content;
imgContent.Children.Add(lbl);
Image i = new Image();
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri("/PieChart3DHH.bmp", UriKind.Relative);
bi.EndInit();
i.Source = bi;
i.Stretch = Stretch.None;
imgContent.Children.Add(i );
button1.Background = scb;
button1.Height = 50;
button1.Width = 100;
button1.Content = imgContent;
DockPanel.SetDock(button1, Dock.Bottom);
button2.Background = scb;
button2.Height = 50;
button2.Width = 100;
button2.Content = content;
button2.Click += new RoutedEventHandler(button2_Click);
DockPanel.SetDock(button2, Dock.Top);
this.dockPanel.Children.Add(button1);
this.dockPanel.Children.Add(button2);
}
private void button2_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("coucou");
}
}
Soit en XAML :
<Window x:Class="WpfApplication1.Window8"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window8" Height="300" Width="300">
<Window.Resources>
<Color A="255" R="0" G="255" B="0" x:Key="scb"/>
<BitmapImage x:Key="bi">
<BitmapImage.UriSource>
/PieChart3DHH.bmp
</BitmapImage.UriSource>
</BitmapImage>
</Window.Resources>
<DockPanel Name="dockPanel" LastChildFill="False">
<DockPanel.Children>
<Button Name="button1" DockPanel.Dock="Bottom">
<Button.Background >
<SolidColorBrush Color="{StaticResource scb}" />
</Button.Background>
<Button.Width>100</Button.Width>
<Button.Height>50</Button.Height>
<Button.Content>
<StackPanel>
<StackPanel.Children>
<Label>
<Label.Content>
Hello
</Label.Content>
</Label>
<Image Source="{StaticResource bi}">
<Image.Stretch>
None
</Image.Stretch>
</Image>
</StackPanel.Children>
</StackPanel>
</Button.Content>
</Button>
<Button Name="button2" DockPanel.Dock="Top" Click="button2_Click">
<Button.Background>
<SolidColorBrush Color="{StaticResource scb}"/>
</Button.Background>
<Button.Width>100</Button.Width>
<Button.Height>50</Button.Height>
<Button.Content>Hello</Button.Content>
</Button>
</DockPanel.Children>
</DockPanel>
</Window>
Pour mieux comprendre le nommage des URI des ressources je vous conseille de jeter un oeil sur le
blog de Thomas LEBRUN.
Une fois que l'image est intégrée en tant que ressource dans le projet, les lignes suivantes sont ajoutées dans le fichier csproj :
<ItemGroup>
<Resource Include="PieChart3DHH.bmp" />
</ItemGroup>
Bien sûr les ressources d'une application sont visibles avec Reflector :