WPF: 纯 XAML 当午餐 Storyboard 基础的绑定条件
WPF: pure XAML when lunch Storyboard base of binding condition
所以我有这个 Storyboard
:
<Storyboard x:Key="animate">
<DoubleAnimation BeginTime="0:0:0" Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:2.0"/>
</Storyboard>
我的绑定value
:
public bool IsFound
{
get { return _isFound; }
set
{
_isFound= value;
NotifyPropertyChanged();
}
}
我的 Grid
得到这个 Storyboard
:
<Grid name="myGrid">
....
<Grid>
if(IsFound)
{
Storyboard storyboard = Resources["animate"] as Storyboard;
if (storyboard != null)
storyboard.Begin(myGrid);
}
所以我正在寻找纯粹的东西 XAML
而不是在代码后面检查这个 IsFound
。
您可以使用 DataTrigger:
<Style TargetType="Grid" x:Key="MyAnimatedGrid">
<Style.Triggers>
<DataTrigger Binding="{Binding IsFound}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard StoryBoard="{StaticResource animate}" />
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
所以我有这个 Storyboard
:
<Storyboard x:Key="animate">
<DoubleAnimation BeginTime="0:0:0" Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:2.0"/>
</Storyboard>
我的绑定value
:
public bool IsFound
{
get { return _isFound; }
set
{
_isFound= value;
NotifyPropertyChanged();
}
}
我的 Grid
得到这个 Storyboard
:
<Grid name="myGrid">
....
<Grid>
if(IsFound)
{
Storyboard storyboard = Resources["animate"] as Storyboard;
if (storyboard != null)
storyboard.Begin(myGrid);
}
所以我正在寻找纯粹的东西 XAML
而不是在代码后面检查这个 IsFound
。
您可以使用 DataTrigger:
<Style TargetType="Grid" x:Key="MyAnimatedGrid">
<Style.Triggers>
<DataTrigger Binding="{Binding IsFound}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard StoryBoard="{StaticResource animate}" />
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>