Bonjour à tous,
Je commence en WPF et je tente de faire un usercontrole.
Le principe de ce usercontol est que j'ai deux images, l'image par défaut et son ombre.
Je définis donc dans une propriété ImagePath l'image qui sera affiché et ImageWidth la largeur de l'image.
Le hic est que mon binding n'est pas mis a jour.
J'ai tenté d'utilisé DependencyProperty.
Puis j'ai tenté tout simplement d'implémenter l'interface INotifyPropertyChanged mais rien n'y fait.
Voici mon XAML
et mon code beind
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
29
30
31
32
33
34 <UserControl x:Class="WpfAnimation.Tools.AnimatedImage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfAnimation" xmlns:tools="clr-namespace:WpfAnimation.Tools"> <UserControl.Resources> <Style TargetType="{x:Type Image}" x:Key="RefletStyle"> <Setter Property="OpacityMask"> <Setter.Value> <LinearGradientBrush EndPoint="0.5,0" StartPoint="0.5,1"> <GradientStop Color="#7F000000" Offset="0"/> <GradientStop Color="#00FFFFFF" Offset="1"/> </LinearGradientBrush> </Setter.Value> </Setter> <Setter Property="RenderTransformOrigin" Value="0.5,0.5"/> <Setter Property="RenderTransform"> <Setter.Value> <TransformGroup> <ScaleTransform ScaleX="1" ScaleY="-0.75"/> </TransformGroup> </Setter.Value> </Setter> </Style> <tools:ImageConverter x:Key="imageConverter" /> </UserControl.Resources> <StackPanel> <Image Name="imgDefault" Width="{Binding ElementName=AnimatedImage, Path=ImageWidth}" Source="{Binding ElementName=AnimatedImage,Converter={StaticResource imageConverter}, Path=ImagePath}"/> <Image Name="imgShadow" Width="{Binding ElementName=AnimatedImage, Path=ImageWidth}" Source="{Binding ElementName=AnimatedImage,Converter={StaticResource imageConverter}, Path=ImagePath}" Style="{StaticResource RefletStyle}"/> </StackPanel> </UserControl>
Mon converter
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public partial class AnimatedImage : UserControl, INotifyPropertyChanged { public string _ImagePath; public string ImagePath { get { return _ImagePath; } set { _ImagePath = value; NotifyPropertyChanged("ImagePath"); } } private int _ImageWidth = 50; public int ImageWidth { get { return _ImageWidth; } set { _ImageWidth = value; NotifyPropertyChanged("ImageWidth"); } } public AnimatedImage() { InitializeComponent(); } #region INotifyPropertyChanged Membres public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } #endregion }
Le but est donc que quand j'ai placé mon composant sur une window et que je lui renseigne le ImagePath et le ImageWidth, le composant soit directement prêt.
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
29
30
31
32 public sealed class ImageConverter : IValueConverter { #region IValueConverter Membres public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { try { return new BitmapImage(new Uri((string)value, UriKind.Absolute)); } catch { return new BitmapImage(); } } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { try { return (value as BitmapImage).BaseUri.LocalPath; } catch { return ""; } } #endregion }
Si vous avez une idée d'où vient le problème n'hésitez pas.
Merci d'avance
Partager