Question

I ask another question to precise one extented need based on first question asked here. @Sheridan point me on the right direction with DataTrigger, but the trigger seems not firing... To sum-up :

  • I re-templated Button control with ControlTemplate (replace the classic button by a colored rectangle and textBlock, which is in ContentPresenter).
  • In ControlTemplate.Triggers, I got one EventTriger to launch width animation on the colored rectangle (this is working)
  • In ControlTemplate.Triggers, as I said, I got one DataTrigger bind to an IsActive property (defined in the ControlTemplate datacontext) to reverse the width animation. This is not working

Here below is my code :

MainMenuView.xaml

<UserControl x:Class="OfficeTourismeBrantome.Views.MainMenuView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         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" 
         d:DesignHeight="800" d:DesignWidth="300">
<UserControl.Resources>
    <Style x:Key="MenuItemButtonStyle" TargetType="Button">
        <Setter Property="FontSize" Value="60" />
        <Setter Property="FontFamily" Value="Segoe" />
        <Setter Property="FontWeight" Value="UltraLight" />
        <Setter Property="Foreground" Value="#FFEBEDEA" />
        <!--<Setter Property="Height" Value="{Binding MenuLayout.MenuItemSize.Height}" />-->
        <Setter Property="HorizontalContentAlignment" Value="Right" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <ControlTemplate.Triggers>
                        <EventTrigger RoutedEvent="Button.Click">
                            <EventTrigger.Actions>
                                <BeginStoryboard>
                                    <Storyboard Name="themeSelectionAnimation">
                                        <DoubleAnimation 
                                            Storyboard.TargetName="coloredRectangle"
                                            Storyboard.TargetProperty="Width"
                                            From="30.0" 
                                            To="250.0" 
                                            Duration="0:0:0.3" />
                                    </Storyboard>
                                </BeginStoryboard>                                    
                            </EventTrigger.Actions>
                        </EventTrigger>
                        <DataTrigger Binding="{Binding IsActive}" Value="False">
                            <DataTrigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation
                                            Storyboard.TargetName="coloredRectangle"            
                                            Storyboard.TargetProperty="Width"
                                            From="250.0" 
                                            To="30.0" 
                                            Duration="0:0:0.3" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </DataTrigger.EnterActions>
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                    <Canvas HorizontalAlignment="Stretch" ClipToBounds="False" >
                        <ContentPresenter Canvas.Left="{Binding MenuLayout.MenuItemLeftMargin}" HorizontalAlignment="Center"                                                  
                                        VerticalAlignment="Center" Canvas.ZIndex="1"/>
                        <TextBlock 
                            Text="{Binding SecondaryText}" 
                            Canvas.Top="50"
                            Canvas.Left="10"
                            FontSize="30"
                            FontWeight="ExtraLight"
                            FontStyle="Italic"
                            Canvas.ZIndex="1"
                            />
                        <Rectangle
                            Canvas.Top="30"
                            Canvas.Left="10"
                            Name="coloredRectangle"
                            Width="30"
                            Height="10"
                            Canvas.ZIndex="0"
                            Fill="{Binding Color}">                                
                        </Rectangle>
                    </Canvas>
                </ControlTemplate>                    
            </Setter.Value>
        </Setter>
    </Style>        
</UserControl.Resources>
<ItemsControl Name="menuButtonContainer" ItemsSource="{Binding Items}" Margin="{Binding MenuLayout.MenuMargin}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>        
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button 
                Style="{StaticResource ResourceKey=MenuItemButtonStyle}" 
                Margin="{Binding ElementName=menuButtonContainer, 
                                    Path=DataContext.MenuLayout.MenuItemMargin}"                    
                Height="{Binding ElementName=menuButtonContainer, 
                                    Path=DataContext.MenuLayout.MenuItemSize.Height}"
                Content="{Binding Text}"                    
                Command="{Binding ElementName=menuButtonContainer, 
                                    Path=DataContext.ChangeThemeCommand}"
                CommandParameter="{Binding Id}"
                />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

MainMenuViewModel :

 /// <summary>
    /// Menu items list
    /// </summary>
    private ObservableCollection<MenuItem> _items;  

 /// <summary>
    /// Menu items list property
    /// </summary>
    public ObservableCollection<MenuItem> Items
    {
        get { return _items; }
        set { _items = value; }
    }

MenuItem class :

 /// <summary>
    /// Handle MenuItem activation state
    /// </summary>
    private bool _isActive;
 /// <summary>
    /// Activation property
    /// </summary>
    public bool IsActive
    {
        get { return _isActive; }
        set 
        {
            // Update simultaneously IsAvailable property
            IsAvailable = !value;
            _isActive = value;
            NotifyPropertyChanged("IsActive");
        }
    }

Thanks a lot for your answers or suggestions

Was it helpful?

Solution

After deeper investigations, I finally gave attention on DataTrigger.EnterActions and ExitActions. In my case, it was exactly that was needed to animate on both value of IsActvie property. This is a XAML-only solution (for brevity sake, I just put back the trigger part) :

<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding IsActive}" Value="True"> <DataTrigger.EnterActions> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="coloredRectangle" Storyboard.TargetProperty="Width" From="30.0" To="250.0" Duration="0:0:0.3" /> </Storyboard> </BeginStoryboard> </DataTrigger.EnterActions> <DataTrigger.ExitActions> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="coloredRectangle"
Storyboard.TargetProperty="Width" From="250.0" To="30.0" Duration="0:0:0.3" />
</Storyboard> </BeginStoryboard> </DataTrigger.ExitActions> </DataTrigger> </ControlTemplate.Triggers>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top