Bonjour,

J'ai un UserControl qui s'ouvre avec une animation.
J'aimerai pouvoir changer la hauteur du contrôle. Le problème est que la hauteur est indiquée dans une DoubleAnimation du Storyboard, et je n'arrive pas à le changer. Je ne peux pas utiliser un Binding sur une dp du contrôle, parce qu'il n'arrive alors plus à freeze() le storyboard.

Voici le code du controle

Drawer.xaml.cs
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
 
using System;
using System.Windows;
using System.Windows.Controls;
 
namespace TestDrawer
{
    /// <summary>
    /// Interaction logic for Drawer.xaml
    /// </summary>
    public partial class Drawer : UserControl
    {
        public Drawer()
        {
            InitializeComponent();
        }
 
        public Visibility Visible
        {
            get { return (Visibility)GetValue(VisibleProperty); }
            set { SetValue(VisibleProperty, value); }
        }
 
        // Using a DependencyProperty as the backing store for Visible.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty VisibleProperty =
            DependencyProperty.Register("Visible", typeof(Visibility), typeof(Drawer), new UIPropertyMetadata(Visibility.Hidden));
 
 
        #region Event handlers
        private void SeparationLabel_Click(object sender, RoutedEventArgs e)
        {
            ToggleVisibility();
        }
        #endregion
 
        public String Title
        {
            get { return (String)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }
 
        // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(String), typeof(Drawer), new UIPropertyMetadata(""));
 
        /// <summary>
        /// Toggle the drawer's visibility (Hidden/Visible).
        /// </summary>
        /// <returns>The new visibility.</returns>
        public Visibility ToggleVisibility()
        {
            if (Visible == Visibility.Hidden)
                Visible = Visibility.Visible;
            else
                Visible = Visibility.Hidden;
 
            return Visible;
        }
 
        /// <summary>
        /// Set  the Visibility of the drawer to Hidden.
        /// </summary>
        /// <returns>'true' is the call leads to the closing of the drawer. 'false' if it is already closed</returns>
        public bool CloseDrawer()
        {
            if (Visible == Visibility.Hidden)
            {
                return false;
            }
 
            Visible = Visibility.Hidden;
            return true;
        }
 
        /// <summary>
        /// Set the Visibility of the drawer to Visible.
        /// </summary>
        /// <returns>'true' is the call leads to the opening of the drawer. 'false' if it is already opened</returns>
        public bool OpenDrawer()
        {
            if (Visible == Visibility.Visible)
            {
                return false;
            }
 
            Visible = Visibility.Visible;
            return true;
        }
 
    }
}
Drawer.xaml

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
52
53
54
55
56
57
58
 
<UserControl x:Class="TestDrawer.Drawer"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestDrawer"
    x:Name="TheDrawer">
    <UserControl.Resources>
        <Storyboard x:Key="OpenDrawerStoryBoard" >
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}" />
            </ObjectAnimationUsingKeyFrames>
            <DoubleAnimation To="150" Storyboard.TargetProperty="Height" Duration="00:00:00.5" />
        </Storyboard>
 
        <Storyboard x:Key="CloseDrawerStoryBoard">
            <DoubleAnimation To="0" Storyboard.TargetProperty="Height" Duration="00:00:00.5" />
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
                <DiscreteObjectKeyFrame KeyTime="0:0:0.5" Value="{x:Static Visibility.Hidden}" />
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
 
        <Style x:Key="DrawerSeparationGridStyle" TargetType="{x:Type Grid}">
            <Setter Property="Visibility" Value="Hidden" />
            <Setter Property="Height" Value="0" />
            <Setter Property="VerticalAlignment" Value="Bottom" />
            <Style.Triggers>
                <Trigger Property="Grid.Tag" Value="{x:Static Visibility.Visible}">
 
                    <Trigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource OpenDrawerStoryBoard}" />
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <BeginStoryboard Storyboard="{StaticResource CloseDrawerStoryBoard}" />
                    </Trigger.ExitActions>
                </Trigger>
            </Style.Triggers>
        </Style>
 
    </UserControl.Resources>
 
    <UserControl.Template>
        <ControlTemplate TargetType="{x:Type local:Drawer}">
            <Grid x:Name="SeparationGrid" Style="{DynamicResource DrawerSeparationGridStyle}" Tag="{TemplateBinding Visible}">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition />
                </Grid.RowDefinitions>
 
                <Button x:Name="SeparationLabel" Style="{DynamicResource ButtonWindowTitleBarStyle}" Grid.Row="0" Height="22" HorizontalAlignment="Stretch" Padding="0" Background="{DynamicResource FilterBarBackground_Brush}" FontSize="12" Content="{TemplateBinding Title}" Click="SeparationLabel_Click" />
                <ContentPresenter Grid.Row="1" />
            </Grid>
        </ControlTemplate>
    </UserControl.Template>
 
    <Grid Background="Tomato">
 
    </Grid>
</UserControl>
et une fenetre :
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
 
<Window x:Class="TestDrawer.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestDrawer"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
 
        <StackPanel Grid.Row="0">
            <Button Click="Button_Click" Content="Open Drawer" />    
        </StackPanel>
 
        <local:Drawer x:Name="TheDrawer" Grid.Row="1">
 
        </local:Drawer>
    </Grid>
</Window>
En cliquant sur le bouton, le contrôle s'ouvre.

Quelqu'un saurait comment rendre paramétrable la hauteur ? Ou une idée pour faire autrement ?

Merci