Bonjour,

J'aurai besoin de votre aide car j'arrive pas à trouver mon erreur.

Je cherche à créer un bouton où on peut changer l'image de manière dynamique. Le but est de pouvoir réutiliser ce model pour tous les boutons de l'application et donc juste de passer les images qui sert pour le Background du bouton en paramètre. Il y a une image pour le bouton normal et une autre pour le bouton Hover.

Voici mon code qui ne fonctionne pas :

Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:Product_Configuration_Tool" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"      
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d">
 
<ControlTemplate x:Key="Template1" TargetType="{x:Type Button}">
<Grid Margin="{TemplateBinding Margin}">
<Image x:Name="img" Source="{Binding local:ImageButton.NormalBackgroundImage, RelativeSource={RelativeSource TemplatedParent}}" Stretch="None"/>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="img" Property="Source" Value="{Binding local:ImageButton.MouseOverBackgroundImage, RelativeSource={RelativeSource TemplatedParent}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ResourceDictionary>

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public static class ImageButton 
{
        public static DependencyProperty NormalBackgroundImageProperty = DependencyProperty.Register(
         "NormalBackgroundImage", typeof(ImageSource), typeof(ImageButton), new PropertyMetadata(""));
 
        public static DependencyProperty MouseOverBackgroundImageProperty = DependencyProperty.Register(
            "MouseOverBackgroundImage", typeof(ImageSource), typeof(ImageButton), new PropertyMetadata(""));
 
        public static ImageSource GetNormalBackgroundImage(DependencyObject obj)
        {
            return (ImageSource)obj.GetValue(NormalBackgroundImageProperty);
        }
 
        public static void SetNormalBackgroundImage(DependencyObject obj, ImageSource value)
        {
            obj.SetValue(NormalBackgroundImageProperty, value);
        }
 
        public static ImageSource GetMouseOverBackgroundImage(DependencyObject obj)
        {
            return (ImageSource)obj.GetValue(MouseOverBackgroundImageProperty);
        }
 
        public static void SetMouseOverBackgroundImage(DependencyObject obj, ImageSource value)
        {
            obj.SetValue(MouseOverBackgroundImageProperty, value);
        }
}
La partie XAML

Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
<Button x:Name="BT_Cancel" Height="30"  Margin="0,0,385,15" Width="110" VerticalAlignment="Bottom" HorizontalAlignment="Right" Template="{StaticResource Template1}" local:ImageButton.NormalBackgroundImage="/Product_Configuration_Tool;component/Images/Btn/closeLightbox_btn.png" local:ImageButton.MouseOverBackgroundImage="/Product_Configuration_Tool;component/Images/Btn/closeLightbox_btn_hover.png" />

Merci d'avance