gpt4 book ai didi

c# - 如果 Triggers/DataTrigger 在 WPF 中不再为真,它会返回到之前的状态吗?

转载 作者:太空狗 更新时间:2023-10-29 21:40:32 24 4
gpt4 key购买 nike

在阅读关于 DataTrigger 的内容时在 MSDN 上,它说

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

意思是

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

但是,这可以推断吗?

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

现在,当 FooProperty 可见时,它会将 mybutton 的 IsEnabled 更改为 True 并将 Opacity 更改为 1 即使我没有编写任何触发器来恢复到以前的值。

这是 DataTrigger 的内置功能,当它不再为真时恢复到以前的值吗?

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

最佳答案

你说的是对的。

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

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

http://msdn.microsoft.com/en-us/library/system.windows.trigger.aspx

“备注部分”

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.

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.

祝你好运:)

关于c# - 如果 Triggers/DataTrigger 在 WPF 中不再为真,它会返回到之前的状态吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12489931/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com