c# - 没有设置器,DataTrigger EnterActions 不起作用?
问题描述
这里 WPF 中的情况真的很奇怪......假设我们在一个窗口中有这些代码:
<Window.Resources>
<Storyboard x:Key="In" Storyboard.TargetProperty="Opacity">
<DoubleAnimation Duration="0:0:1" To="1"/>
</Storyboard>
<Storyboard x:Key="Out" Storyboard.TargetProperty="Opacity">
<DoubleAnimation Duration="0:0:1" To="0"/>
</Storyboard>
<Style x:Key="style" TargetType="FrameworkElement">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=MainWin, Path=Checked}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource Out}"/>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource In}"/>
</DataTrigger.ExitActions>
<!--<Setter Property="Tag" Value="{x:Null}"/>-->
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<StackPanel.Resources>
<Style TargetType="TextBlock" BasedOn="{StaticResource style}"/>
</StackPanel.Resources>
<CheckBox Content="CheckBox bound to MainWindow.Checked property"
IsChecked="{Binding ElementName=MainWin, Path=Checked}"/>
<TextBlock Text="Tick above should hide me"/>
</StackPanel>
MainWindow.cs 中有一个依赖属性,名为Checked
. 干净利落:
public bool Checked {
get { return (bool)GetValue(CheckedProperty); }
set { SetValue(CheckedProperty, value); }
}
public static readonly DependencyProperty CheckedProperty =
DependencyProperty.Register("Checked", typeof(bool), typeof(MainWindow), new PropertyMetadata(false));
基本上应该在选中TextBlock
时隐藏。CheckBox
但...
Setter
除非我取消注释for ,否则上面的代码不起作用Tag
!(实际上不必是 Tag,但任何 Setter 都可以。)
如果我像这样直接设置它,它确实可以在不取消注释的情况下工作:Style
TextBlock
<TextBlock Text="Tick above should hide me" Style="{StaticResource style}"/>
谁能解释这种奇怪的行为?我是否在框架中遇到某种错误?
解决方案
推荐阅读
- https - Istio:如何在 Minikube 上的服务之间设置 HTTPS 流量?
- python - 字符串对象没有属性名称 - 泰坦尼克号数据科学
- python - 加载保存的模型后得到错误的预测
- python-3.x - 无法在jupyter中导入python包
- google-apps-script - onFormSubmit 触发器发送最后一行提交以同时提交
- android - 改造响应无法处理空间
- r - 计算R中单词的出现次数,总单词和总唯一单词
- c++ - 我可以从 divide_typeof_helper 中检索基本单位吗?
- maven - Maven 删除文件阶段验证除了选定的文件
- ios - 需要从 open api 获取国家名称