如果您在自己Trigger
的属性上使用 a ,UIElement
您将不想使用Trigger
not DataTrigger
,DataTriggers
更多用于DataObjects
模型等的更改,normalTriggers
用于UIElements
等。
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<Trigger Property="Visibility" Value="Visible">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource FlashErrorImage}" />
</Trigger.EnterActions>
<Trigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="FlashErrorImage" />
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
</Image.Style>
这是一个完整的工作模型,如果它有助于缩小问题范围
xml:
<Window x:Class="WpfApplication7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Name="UI">
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
<Storyboard x:Key="FadeAnimation" Storyboard.TargetProperty="Opacity" >
<DoubleAnimation From="0" To="1" Duration="0:0:5" />
</Storyboard>
</Window.Resources>
<StackPanel>
<Image Source="http://icons.iconarchive.com/icons/iconcreme/halloween/128/Skeleton-icon.png"
Visibility="{Binding ElementName=UI, Path=ImageVisible, Converter={StaticResource BooleanToVisibilityConverter}}" Height="247">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<Trigger Property="Visibility" Value="Visible">
<Trigger.EnterActions>
<BeginStoryboard Name="Fade" Storyboard="{StaticResource FadeAnimation}" />
</Trigger.EnterActions>
<Trigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="Fade" />
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
<Button Click="Button_Click" Content="Visible"/>
</StackPanel>
</Window>
代码:
public partial class MainWindow : Window, INotifyPropertyChanged
{
private bool _imageVisible;
public MainWindow()
{
InitializeComponent();
}
public bool ImageVisible
{
get { return _imageVisible; }
set { _imageVisible = value; NotifyPropertyChanged("ImageVisible"); }
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ImageVisible = !ImageVisible;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string p)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(p));
}
}
}