WPF DataTrigger用于重新模板化的子控件

时间:2014-03-12 10:45:20

标签: c# wpf xaml mvvm datatrigger

我根据第一个问题asked here提出另一个问题,以确切地满足一个需求。 @Sheridan用DataTrigger向我指出正确的方向,但触发器似乎没有触发...... 总结一下:

  • 我使用ControlTemplate重新模板化Button控件(用彩色矩形替换经典按钮,在ContentPresenter中替换textBlock)。
  • 在ControlTemplate.Triggers中,我有一个EventTriger在彩色矩形上启动宽度动画(这是有效的)
  • 在ControlTemplate.Triggers中,正如我所说,我有一个DataTrigger绑定到IsActive属性(在ControlTemplate datacontext中定义)来反转宽度动画。这不起作用

以下是我的代码:

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类:

 /// <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");
        }
    }

非常感谢您的回答或建议

1 个答案:

答案 0 :(得分:0)

经过深入调查后,我终于关注DataTrigger.EnterActions和ExitActions。就我而言,正是需要对IsActvie属性的两个值进行动画处理。这是一个仅限XAML的解决方案(为了简洁起见,我只是放回触发器部分):

<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>