简体   繁体   English

DataTrigger故事板未在PropertyValueChange上触发WPF

[英]DataTrigger storyboard not firing WPF on PropertyValueChange

So forgive me ahead of time if this has been answered before and I just haven't been able to see it for the life of me, but I have been searching for the past 4 hours with no avail. 因此,如果以前已经回答过这个问题,请提前原谅我,但我一生中一直看不到它,但是过去4个小时我一直无济于事。

I am trying to get a DataTrigger to fire on the change of a single bool property. 我正在尝试让DataTrigger在单个bool属性的更改上触发。 The DataTrigger is to begin a storyboard that simply just adjusts the window size. DataTrigger将开始一个故事板,只需调整窗口大小即可。

Here is the XAML: 这是XAML:

<Window.Resources>
    <local:SatisfactionsViewModel x:Key="SatisfactionsViewModel"/>
    <!--The storyboard animation-->
    <Storyboard x:Key="windowFadeUp">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="Satisfaction_Processing">
            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="171"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</Window.Resources>
<!--The Datatrigger that doesn't seem to work, probably my own stupidity-->
<Window.Style>
    <Style>
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsWorkingOnStuff, NotifyOnSourceUpdated=True}" Value="true">
                <DataTrigger.EnterActions>
                    <BeginStoryboard Storyboard="{StaticResource windowFadeUp}"/>
                </DataTrigger.EnterActions>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Style>

Here is the relevant C# from the ViewModel: 这是ViewModel中的相关C#:

    private bool isWorkingOnStuff;

    public bool IsWorkingOnStuff
    {
        get { return isWorkingOnStuff; }
        set
        {
            if (value == isWorkingOnStuff)
            { return; }
            isWorkingOnStuff = value;
            OnPropertyChanged();
        }
    }
    //Other non-relative stuff
   //PropertyChangedEvent handler
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged([CallerMemberName]string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
   //Bunch of other stuff not related
   private void ProcessCommand()
    { 
        if (fileName == null || fileName == "")
        {
            MessageBox.Show("Please select a file. \nIt won't work without one... -_-");
        }
        else
        {
            IsWorkingOnStuff = true;
            IsProcessEnabled = false;
            IsProgressBarVisibile = Visibility.Visible;
        //more business logic goes here...
        }
   }

So as I understand it, when the IsWorkingOnStuff property changes to true, the datatrigger should fire, the storyboard execute, and rejoicing happen...but it's not. 因此,据我了解,当IsWorkingOnStuff属性更改为true时,数据触发器应触发,情节提要执行,并发生欢乐...但事实并非如此。

Any idea where I am going wrong? 知道我要去哪里错了吗?

Thank you ahead of time. 提前谢谢你。

Remove the Storyboard.TargetName attribute and make sure that you have set the DataContext of the window to an instance of the SatisfactionsViewModel class: 删除Storyboard.TargetName属性,并确保已将窗口的DataContext设置为SatisfactionsViewModel类的实例:

<Window x:Class="WpfApplication1.MainWindow"
        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"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="300" Width="300" x:Name="Satisfaction_Processing">
    <Window.DataContext>
        <local:SatisfactionsViewModel />
    </Window.DataContext>
    <Window.Resources>
        <!--The storyboard animation-->
        <Storyboard x:Key="windowFadeUp">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)">
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="171"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Window.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsWorkingOnStuff, NotifyOnSourceUpdated=True}" Value="true">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource windowFadeUp}"/>
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Style>
    <StackPanel>

    </StackPanel>
</Window>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM