当窗口获得焦点时,我想要发生一些事情。但是,这似乎不起作用:
<Window x:Class="Sample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" Background="Black"
Opacity="0.5">
<Grid>
</Grid>
<Window.Style>
<Style>
<Style.Triggers>
<Trigger Property="Window.IsActive" Value="true">
<Setter Property="Control.Background" Value="Blue" />
<Setter Property="Window.Title" Value="Testing" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Style>
</Window>
另一方面,如果我用{Enter,Exit}Action
上的动画替换setter似乎有效。
<Window x:Class="Sample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" Background="Black"
Opacity="0.5">
<Grid>
</Grid>
<Window.Style>
<Style>
<Style.Triggers>
<Trigger Property="Window.IsActive" Value="true">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="Background.Color"
To="Blue" Duration="0:0:1" />
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Title">
<DiscreteObjectKeyFrame Value="Testing" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="Background.Color"
To="Black" Duration="0:0:1" />
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Title">
<DiscreteObjectKeyFrame Value="Window1" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
</Window.Style>
</Window>
我认为这种解决方法足以满足我的目的,但我想理解为什么“简单”的方法不起作用。
P.S。我不能完全理解语法突出显示...它似乎在几个级别的缩进后放弃。
答案 0 :(得分:2)
如果要根据某些条件使用触发器切换某些属性值,则需要在样式本身中设置这些属性的默认值,否则无论您在setter中设置了什么值,属性的值都将始终被覆盖由dependency property value precedence引起的这些属性的本地值。在您的情况下,您需要在您的样式中设置Background
和Title
的值,如下所示 -
<Window x:Class="Sample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300" Opacity="0.5">
<Grid>
</Grid>
<Window.Style>
<Style>
<Setter Property="Control.Background" Value="Black"/>
<Setter Property="Window.Title" Value="Window1" />
<Style.Triggers>
<Trigger Property="Window.IsActive" Value="true">
<Setter Property="Control.Background" Value="Blue" />
<Setter Property="Window.Title" Value="Testing" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Style>
</Window>
此外,您可以省略设置值,让触发器为您设置这些值。这也可以工作(窗口声明时省略背景和标题) -
<Window x:Class="Sample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300" Opacity="0.5">
<Grid>
</Grid>
<Window.Style>
<Style>
<Style.Triggers>
<Trigger Property="Window.IsActive" Value="true">
<Setter Property="Control.Background" Value="Blue" />
<Setter Property="Window.Title" Value="Testing" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Style>
</Window>