我有一个WPF页面。页面有一些内容,但页面根布局的最后一个子组件是我创建的用户控件。它看起来像这样:
<UserControl DataContext=UserControlViewModel>
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="visibilityConverter" />
</UserControl.Resources>
<Grid
Name="grid"
Visibility="{Binding IsOn, Converter={StaticResource visibilityConverter}}">
<!-- Border to dim everything behind my user control -->
<Border Background="#000000" Opacity="0.4" />
<!-- The following border is red and holds the content -->
<Border
Width="{Binding ElementName=txt, Path=ActualWidth}"
Height="{Binding ElementName=txt, Path=ActualHeight}"
Margin="{Binding ElementName=txt, Path=Margin}"
HorizontalAlignment="{Binding ElementName=txt, Path=HorizontalAlignment}"
VerticalAlignment="{Binding ElementName=txt, Path=VerticalAlignment}"
Background="Red">
<TextBlock
Name="txt"
Width="200"
Height="100"
Margin="20"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="This is my super awesome message!" />
</Border>
</Grid>
</UserControl>
默认情况下,IsOn
对象的UserControlViewModel
属性设置为false
,即用户控件不可见。我已经实现了一些逻辑,将此属性更改为true
,然后用户控件显示在所有其他变暗的组件的前面。这很好用。
现在,我想创建一个淡入淡出效果动画,一旦它变得可见,它就会使用户控件后面的组件变暗。接下来,我想让我的红色边框保持内容从左侧淡入,因此移动+淡入淡出。
让我们先从淡入淡出效果开始。我把这种风格写到Border
,它应该用来调暗背景成分:
<UserControl DataContext=UserControlViewModel>
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="visibilityConverter" />
</UserControl.Resources>
<Grid
Name="grid"
Visibility="{Binding IsOn, Converter={StaticResource visibilityConverter}}">
<!-- Border to dim everything behind my user control -->
<Border Background="#000000" Opacity="0.4">
<!-- The following style is new code -->
<Border.Style>
<Style TargetType="Border">
<Style.Triggers>
<Trigger Property="{Binding ElementName=grid, Visibility}" Value="Visible">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="Opacity"
From="0.0"
To="0.4"
Duration="0:0:1" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
...
但问题是:我无法在触发器Property
上设置绑定,因为它不是依赖属性。 grid
将Visibility
属性设置为Visible
后,我需要一种方法来告诉我的触发器。请帮助,谢谢!
第二个问题是,我不知道如何移动红色边框,所以我需要帮助进行一些比例变换,我想......再次感谢!
答案 0 :(得分:16)
尝试替换以下行:
原件:
<Trigger Property="{Binding ElementName=grid, Visibility}" Value="Visible">
的更换:
<DataTrigger Binding={Binding Visibility, ElementName=grid} Value="Visibile">