WPF 中的数据触发器问题
本文关键字:触发器 问题 数据 WPF | 更新日期: 2023-09-27 18:37:28
我在获取数据触发器以调用 WPF 中的故事板时遇到问题。我已经为此工作了近 3 个小时,所以希望有人可以提供帮助!
这是我的 XAML 代码段,其中包含情节提要/触发器。
<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;
}
}
IsLoginWrong 必须是依赖项属性或实现 INotifyPropertyChanged 的属性,以便可以通知触发器它已更改。
如何实现 INotifyPropertyChanged: https://msdn.microsoft.com/library/ms229614(v=vs.100).aspx