Bonjour a tous,
Je suis débutant en WPF et suis confronté à un problème qui semble trivial mais pour lequel je n'ai pas trouvé de réponse sur le net.
Voilà mon problème:
je souhaite réaliser une toolbar qui se cache avec une animation lorsque l'on clique sur un bouton et qui réapparait lorsqu'on clique sur se même bouton.
Voilà la manière dont je procède:
j'ai un view-model qui implémente INotifyProperty qui propose une propriété"Visible".
Cette propriétéest modifiée par une commande "VisibleChangedCmd" comme le montre le code qui suit:
Ma propriété "Visible" est bien modifée sur commande donc pas de problème de ce coté là.
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 public class ccAutoHideToolBarViewModel : ViewModelBase { private ccAutoHideToolBarModel m_model; private bool m_bVisible; public bool Visible { get { return m_bVisible; } set { if (m_bVisible != value) { m_bVisible = value; RaisePropertyChanged("Visible"); } } } private CommandImpl m_VisibleChangedCmd; public ICommand VisibleChangedCmd { get { return m_VisibleChangedCmd; } } public ccAutoHideToolBarViewModel() { m_model = new ccAutoHideToolBarModel(); m_VisibleChangedCmd = new CommandImpl(ExecuteVisibleChangedCmd, CanExecuteVisibleChangedCmd); } #region command public bool CanExecuteVisibleChangedCmd(object parameter) { return true; } public void ExecuteVisibleChangedCmd(object parameter) { Visible = !Visible; } #endregion }
J'ai maintenant une vue composée du fichier .xaml et du code behind associé.
Dans mon code behind, je déclare une dependency property "IsExtended":
Sur changement de cette DP "IsExtended", je lance un animation qui vient cacher ou faire apparaître ma tool bar fonction de sa valeur.
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 public partial class ccAutoHideToolBar : UserControl { public bool IsExtended { get { return (bool)this.GetValue(IsExtendedProperty); } set { this.SetValue(IsExtendedProperty, value); } } public static readonly DependencyProperty IsExtendedProperty = DependencyProperty.Register( "IsExtended", typeof(bool), typeof(ccAutoHideToolBar), new PropertyMetadata(false, OnIsExtendedChanged)); public ccAutoHideToolBar() { this.InitializeComponent(); // Insert code required on object creation below this point. } protected static void OnIsExtendedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ccAutoHideToolBar toolbar = (ccAutoHideToolBar)d; bool visible = (bool)e.NewValue; Storyboard animation; if(visible) animation = (Storyboard)toolbar.Resources["appearToolBar"]; else animation = (Storyboard)toolbar.Resources["vanishToolBar"]; animation.Begin(); } }
Mon problème est le suivant, je ne parviens pas à faire le binding entre ma DP "IsExtended" de ma vue et ma propriété Visible de mon Vue-Model.
Les seuls exemples que je rencontre traitent d'un binding entre une DP d'un objet composant ma vue et une propriété du model. (<TextBox Content={Binding TextVM}/> par exemple)
Voilà mon .xaml:
Merci d'avance pour ceux s'attarderont sur mon problème!
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
51 <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" xmlns:local="clr-namespace:WpfCustomControlLibraryV5" xmlns:vm="clr-namespace:WpfCustomControlLibraryV5.ViewModels" x:Class="WpfCustomControlLibraryV5.ccAutoHideToolBar" x:Name="AutoHideToolBar" d:DesignWidth="640" d:DesignHeight="480" RenderTransformOrigin="0.5,0.5"> <UserControl.Resources> <vm:ccAutoHideToolBarViewModel x:Key="ccAutoHideToolBarViewModel"/> <Storyboard x:Key="appearToolBar"> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="LayoutRoot"> <EasingDoubleKeyFrame KeyTime="0" Value="50"/> <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="0"/> </DoubleAnimationUsingKeyFrames> </Storyboard> <Storyboard x:Key="vanishToolBar"> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="AutoHideToolBar"> <EasingDoubleKeyFrame KeyTime="0" Value="0"/> <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="50"/> </DoubleAnimationUsingKeyFrames> </Storyboard> </UserControl.Resources> <UserControl.DataContext> <vm:ccAutoHideToolBarViewModel/> </UserControl.DataContext> <Grid x:Name="LayoutRoot"> <Grid.ColumnDefinitions> <ColumnDefinition Width="24"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <Button x:Name="actionButton" Grid.Column="0" VerticalAlignment="Center" Command="{Binding VisibleChangedCmd}" Background="Transparent" BorderBrush="Transparent" Focusable="False">Toolbox <Button.LayoutTransform> <RotateTransform Angle="-90"></RotateTransform> </Button.LayoutTransform> </Button> <ToolBarPanel x:Name="toolBarPanel" Grid.Column="1" Width="50" VerticalAlignment="Center"> <Button>Test1 </Button> <Button>Test2 </Button> <Button>Test3 </Button> <Button>Test4 </Button> </ToolBarPanel> </Grid> </UserControl>
Partager