繁体   English   中英

DataTrigger故事板未在PropertyValueChange上触发WPF

[英]DataTrigger storyboard not firing WPF on PropertyValueChange

因此,如果以前已经回答过这个问题,请提前原谅我,但我一生中一直看不到它,但是过去4个小时我一直无济于事。

我正在尝试让DataTrigger在单个bool属性的更改上触发。 DataTrigger将开始一个故事板,只需调整窗口大小即可。

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

这是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...
        }
   }

因此,据我了解,当IsWorkingOnStuff属性更改为true时,数据触发器应触发,情节提要执行,并发生欢乐...但事实并非如此。

知道我要去哪里错了吗?

提前谢谢你。

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