在StoryBoard中设置DependencyProperty值

时间:2014-08-05 09:11:35

标签: c# wpf storyboard dependency-properties datatrigger

我想从ViewModel层触发动画,所以我创建了一个布尔DependencyProperty和一个绑定到它的DataTrigger。

动画结束后,我想将DependencyProperty值重置为False,这样我就可以再次触发动画,但这似乎不起作用,DependencyProperty永远不会设置为False。

DP声明:

public bool SlidingInLeft {
    get { return (bool)GetValue(SlidingInLeftProperty); }
    set {
        Console.WriteLine("SlidingInLeft: " + value);
        SetValue(SlidingInLeftProperty, value);
    }
}

public static readonly DependencyProperty SlidingInLeftProperty =
    DependencyProperty.Register("SlidingInLeft", typeof(bool), typeof(OperationsListPageVM), new PropertyMetadata(false));

XAML:

<DataTrigger Binding="{Binding SlidingInLeft}" Value="True">
    <DataTrigger.EnterActions>
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation Storyboard.TargetProperty="(UserControl.RenderTransform).(TranslateTransform.X)" SpeedRatio=".25" From="2000" To="0" Duration="0:0:.25" />
                <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(UserControl.Visibility)">
                    <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}"/>
                </ObjectAnimationUsingKeyFrames>
            </Storyboard>
        </BeginStoryboard>
    </DataTrigger.EnterActions>
    <DataTrigger.ExitActions>
        <BeginStoryboard>
            <Storyboard>
                <BooleanAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="SlidingInLeft">
                    <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="False"/>
                </BooleanAnimationUsingKeyFrames>
            </Storyboard>
        </BeginStoryboard>
    </DataTrigger.ExitActions>
</DataTrigger>

(请注意,DataContext设置为ViewModel对象)

编辑:&#34;整体&#34;代码,如果它可以提供任何帮助:

OperationsListPageVM.cs:

namespace MaintenanceModule.ViewModel {
    public class OperationsListPageVM : DependencyObject, INotifyPropertyChanged {
        #region propriétés
        public ObservableCollection<OperationVM> AllOperations { get; set; }
        public ObservableCollection<OperationVM> DisplayedOperations { get; set; }
        public static OperationsListPageVM Instance { get; private set; }

        public IPageVM PreviousPage { get; set; }
        public IPageVM NextPage { get; set; }

        public bool SlidingInLeft {
            get { return (bool)GetValue(SlidingInLeftProperty); }
            set {
                Console.WriteLine("SlidingInLeft: " + value);
                SetValue(SlidingInLeftProperty, value);
            }
        }


        public static readonly DependencyProperty SlidingInLeftProperty =
            DependencyProperty.Register("SlidingInLeft", typeof(bool), typeof(OperationsListPageVM), new PropertyMetadata(false));
        #endregion

        #region singleton
        private OperationsListPageVM() {
            AllOperations = new ObservableCollection<OperationVM>();
            AllOperations.CollectionChanged += (sender, args) => {
                switch (args.Action) {
                    case NotifyCollectionChangedAction.Add:
                        foreach (OperationVM ovm in args.NewItems) {
                            DisplayedOperations.Add(ovm);
                        }
                        break;
                }
            };
            DisplayedOperations = new ObservableCollection<OperationVM>();
        }

        public static OperationsListPageVM GetInstance() {
            return Instance ?? (Instance = new OperationsListPageVM());
        }

        #endregion

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void onPropertyChanged(string propertyName) {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

OperationsListPageV.xaml:

<UserControl x:Class="MaintenanceModule.View.Pages.OperationsListPageV"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:lib="clr-namespace:TactileWPFComponents;assembly=TactileWPFComponents"
             xmlns:viewModel="clr-namespace:MaintenanceModule.ViewModel"
             DataContext="{Binding RelativeSource={RelativeSource self}, Path=OperationsListPage}">
    <UserControl.Style>
        <Style>
            <Setter Property="UserControl.RenderTransform">
                <Setter.Value>
                    <TranslateTransform/>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding SlidingInLeft}" Value="True">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="(UserControl.RenderTransform).(TranslateTransform.X)" SpeedRatio="1" From="2000" To="0" Duration="0:0:.25" />
                                <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(UserControl.Visibility)">
                                    <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}"/>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                    <DataTrigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <BooleanAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="SlidingInLeft">
                                    <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="False"/>
                                </BooleanAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.ExitActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Style>

    <Grid>
        <Grid.Background>
            <ImageBrush ImageSource="../../Resources/Images/GlassBlockZoneRecipeFlow.png"/>
        </Grid.Background>

        <lib:ScrollViewerFriction Margin="0, 20">
            <lib:WatchList ItemsSource="{Binding DisplayedOperations}" Margin="20">
                <lib:WatchList.Resources>
                    <LinearGradientBrush x:Key="{x:Static SystemColors.ControlBrushKey}">
                        <GradientStop Offset="0.0" Color="Transparent"/>
                        <GradientStop Offset="0.25" Color="Gray"/>
                        <GradientStop Offset="0.5" Color="Transparent"/>
                        <GradientStop Offset="0.75" Color="Gray"/>
                        <GradientStop Offset="1.0" Color="Transparent"/>
                    </LinearGradientBrush>
                    <Style x:Key="StyleListBoxItem" TargetType="ListBoxItem">
                        <Style.Resources>
                            <LinearGradientBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" StartPoint="0,0" EndPoint="0,1">
                                <LinearGradientBrush.RelativeTransform>
                                    <RotateTransform Angle="-90"/>
                                </LinearGradientBrush.RelativeTransform>
                                <GradientStop Offset="0.0" Color="Transparent"/>
                                <GradientStop Offset="0.25" Color="Gray"/>
                                <GradientStop Offset="0.5" Color="Transparent"/>
                                <GradientStop Offset="0.75" Color="Gray"/>
                                <GradientStop Offset="1.0" Color="Transparent"/>
                            </LinearGradientBrush>
                        </Style.Resources>
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                    </Style>
                </lib:WatchList.Resources>
                <ListBox.BorderThickness>0</ListBox.BorderThickness>
                <ListBox.ItemContainerStyle>
                    <Style BasedOn="{StaticResource StyleListBoxItem}" TargetType="ListBoxItem"/>
                </ListBox.ItemContainerStyle>
                <lib:WatchList.ItemTemplate>
                    <DataTemplate DataType="viewModel:OperationVM">

                        <Grid Margin="0, 10">

                            <Grid.Resources>
                                <Style TargetType="TextBlock">
                                    <Setter Property="Foreground" Value="#050670"/>
                                    <Setter Property="FontSize" Value="15"/>
                                    <Setter Property="TextAlignment" Value="Center"/>
                                    <Setter Property="TextWrapping" Value="WrapWithOverflow"/>
                                    <Setter Property="VerticalAlignment" Value="Center"/>
                                </Style>
                            </Grid.Resources>

                            <Grid.Background>
                                <ImageBrush ImageSource="../../Resources/Images/Frame4.png"/>
                            </Grid.Background>

                            <Grid.RowDefinitions>
                                <RowDefinition/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="10"/>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>

                            <Grid Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Background="{Binding Color}"/>

                            <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Name}"/>
                            <TextBlock Grid.Row="0" Grid.Column="2" Text="{Binding CycleDuration}"/>
                            <TextBlock Grid.Row="1" TextElement.FontSize="8" HorizontalAlignment="Left"  Grid.Column="1" Text="{Binding EstimatedDate, StringFormat='{}{0:D}'}"/>

                            <StackPanel Grid.Row="1" Grid.Column="2">
                                <ProgressBar Height="20" Width="100" Maximum="100" Minimum="0" Orientation="Horizontal" Value="{Binding CyclePercentage}"/>
                                <TextBlock Text="{Binding CyclePercentage, StringFormat=\{0:N0\}%}"/>
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </lib:WatchList.ItemTemplate>
            </lib:WatchList>
        </lib:ScrollViewerFriction>
    </Grid>
</UserControl>

OperationsListPageV.xaml.cs:

namespace MaintenanceModule.View.Pages {

    public partial class OperationsListPageV : UserControl, IPageV {
        public OperationsListPageVM OperationsListPage { get; set; }

        public OperationsListPageV() {
            OperationsListPage = OperationsListPageVM.GetInstance();
            InitializeComponent();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个:

在XAML中

<Storyboard Completed="Storyboard_Completed" >
在codebehind中

private void Storyboard_Completed(object sender, EventArgs e)
{
   SetValue(SlidingInLeftProperty, false);
}