WPF中DataTrigger的问题

时间:2016-03-04 16:50:01

标签: c# wpf xaml storyboard datatrigger

我在使用DataTrigger调用WPF中的Storyboard时遇到问题。我已经为此工作了将近3个小时,所以希望有人可以提供帮助!

这是我的XAML Snippet,其中包含Storyboard / Trigger。

<Style x:Key="image" TargetType="{x:Type Image}">
    <Style.Resources>
        <Storyboard x:Key="LoadTitleScreen">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="image">
                <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="0"/>
            </DoubleAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="image">
                <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
                <DiscreteObjectKeyFrame KeyTime="0:0:0.6" Value="{x:Static Visibility.Collapsed}"/>
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </Style.Resources>
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=IsLoginWrong}" Value="true">
            <DataTrigger.EnterActions>
                    <BeginStoryboard Storyboard="{StaticResource LoadTitleScreen}"/>
            </DataTrigger.EnterActions>
        </DataTrigger>
    </Style.Triggers>
</Style>

这是我的C#代码,用于设置DataTrigger的值:

    public partial class MainWindow : Window, INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    private Boolean _isLoginWrong;
    public Boolean IsLoginWrong
    {
        get { return this._isLoginWrong; }

        set
        {
            if (value != this._isLoginWrong)
            {
                this._isLoginWrong = value;
                NotifyPropertyChanged("IsLoginWrong");
            }
        }
    }

    Timer ConnectToServer = new Timer(3000);


    public MainWindow()
    {
        InitializeComponent();
        ConnectToServer.Elapsed += ConnectToServer_Elapsed;
        ConnectToServer.Start();
    }

    void ConnectToServer_Elapsed(object sender, ElapsedEventArgs e)
    {
        IsLoginWrong = true;
    }
}

1 个答案:

答案 0 :(得分:1)

IsLoginWrong must be a dependency property or a property that implements INotifyPropertyChanged so your Trigger can be notified that it has changed.

How to implement INotifyPropertyChanged: https://msdn.microsoft.com/library/ms229614(v=vs.100).aspx