在我看来,Trigger
在Interactivity命名空间中找到的Blend-style 与在WPF中Trigger
通过Styles,ControlTemplate
s等提供的经典s 之间存在重大差异(我认为这可能也适用于SilverLight).
在WPF中,当您Trigger
使用Setter 设置a 时,您会得到两种行为:如果满足触发条件,则应用Setter.但是,一旦不再满足触发器,则恢复先前的值.这在编程UI时非常有用.
我尝试DataTrigger
使用Blend方式编写类似的代码:我将ChangePropertyAction应用于我的控件,并DataTrigger
使用绑定到我的ViewModel来触发Interactivity .
<Rectangle x:Name="SecondaryHighlightBackground" Fill="#FF505050">
<i:Interaction.Triggers>
<ei:DataTrigger Binding="{Binding IsHighlighted}" Value="Value">
<ei:ChangePropertyAction PropertyName="Opacity" Value="0.5"/>
</ei:DataTrigger>
</i:Interaction.Triggers>
...
</Rectangle>
Run Code Online (Sandbox Code Playgroud)
所以这按预期工作......除了我惊讶地发现,当我将IsHighlighted值翻转为假时,原始不透明度0%未恢复(我将其设置为较低).为了仔细检查这个,我写了这个例子来验证:
<Window x:Class="TestWpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window"
Title="MainWindow"
Width="640"
Height="480">
<Grid x:Name="LayoutRoot">
<Button Name="button1"
Width="173"
Height="57"
Margin="10,10,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Content" Value="foo" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsMouseOver, ElementName=button1}" Value="True">
<Setter Property="Content" Value="bar" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
预期的行为是,如果您将鼠标悬停在按钮上,则内容将更改为"bar".移动鼠标将内容恢复为'Foo',如Style所指定.样式设置内容以避免属性优先级问题.
我的问题是:Blend扩展中是否真的缺少双向触发,还是有办法以某种方式启用它? …