如果在WPF中不再将Triggers/DataTrigger返回到以前的状态,是否会返回到以前的状态? [英] Do Triggers/DataTrigger return to previous state if it is no longer true in WPF?

查看:60
本文介绍了如果在WPF中不再将Triggers/DataTrigger返回到以前的状态,是否会返回到以前的状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MSDN上阅读有关 DataTrigger 的同时,说

While reading about DataTrigger on MSDN, it says

代表一个触发器,当绑定数据满足指定条件时,该触发器将应用属性值或执行操作.

Represents a trigger that applies property values or performs actions when the bound data meets a specified condition.

它表示

当触发器为true时,它将值更改为所需值.

When a trigger is true it changes the value to the desired value.

但是,可以推断出来吗?

But, can this be inferred?

当它不再为true时,它将值返回到先前的值.

When it is no longer true it returns the value to the previous value.


我如何得出这个结论

我做到了

<Style x:Key="fd" TargetType="SomeControl">
 <Setter Property="Control.Template">
  <Setter.Value>
   <ControlTemplate>
    <Button Content="Foo" x:Name="mybutton">
    <ControlTemplate.Triggers>
     <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=FooProperty}" 
                  Value="Collapsed">
      <Setter Property="IsEnabled" Value="False" TargetName="mybutton"/>
      <Setter Property="Opacity" Value="0.5" TargetName="mybutton"/>
     </DataTrigger>
    </ControlTemplate.Triggers>
   </ControlTemplate>
  </Setter.Value>
 </Setter>
</Style>

FooProperty 折叠时,它将mybutton的 IsEnabled 更改为 False ,将 Opacity 更改为 0.5

When FooProperty becomes collapsed it changes mybutton's IsEnabled to False and Opacity to 0.5

现在,当 FooProperty 变为可见时,它将mybutton的 IsEnabled 更改为 True ,将 Opacity 更改为 1 ,尽管我还没有编写任何触发器以恢复到先前的值.

Now when FooProperty becomes visible it changes mybutton's IsEnabled to True and Opacity to 1 even though I have not written any trigger for reverting back to previous values.

这是否是 DataTrigger 的内置功能,当它不再为真时可以还原为先前的值吗?

Is this an inbuilt feature of DataTrigger to revert back to previous value When it is no longer true?

如果是,是否有任何Microsoft/MSDN文档来证明这一点?

If yes, is there any Microsoft/MSDN doc to prove this?

推荐答案

您所说的是正确的.

至于证明这一点的实际文档,从这里搜索:

As for actual doc to prove this, search from here:

http://msdn.microsoft.com/en-us/library/ms745683.aspx

http://msdn.microsoft.com/zh-我们/library/system.windows.trigger.aspx

备注部分"

WPF定义了与最终用户操作相对应的属性,例如当用户将鼠标悬停时,将IsMouseOver属性设置为true将光标移到UIElement或相应的IsMouseOver属性上ContentElement.在属性值中表示最终用户的操作,与Trigger元素一起,允许WPF样式更改属性基于这些最终用户操作的值,全部来自标记内.

WPF defines properties that correspond to end-user actions, such as the IsMouseOver property that is set to true when the user hovers the cursor over a UIElement or the corresponding IsMouseOver property of a ContentElement. Representing end-user actions in property values, along with the Trigger element, allows WPF styles to change property values based on those end-user actions, all from within markup.

由触发器更改的属性将自动重置为其不再满足触发条件时的先前值.触发器针对预期的瞬态进行了优化更改并返回到原始状态,例如"IsPressed on Button"和在ListBoxItem上被IsSelected.感兴趣的财产必须是依赖项属性.

The properties changed by triggers are automatically reset to their previous value when the triggered condition is no longer satisfied. Triggers are optimized for transient states which are expected to change and return to original state, such as IsPressed on Button and IsSelected on ListBoxItem. The Property of interest must be a dependency property.

请注意,您必须在触发使触发有意义.如果一个或两个属性未设置,将引发异常.

Note that you must specify both the Property and Value properties on a Trigger for the trigger to be meaningful. If one or both of the properties are not set, an exception is thrown.

祝你好运:)

这篇关于如果在WPF中不再将Triggers/DataTrigger返回到以前的状态,是否会返回到以前的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆