Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Triggers/DataTrigger return to previous state if it is no longer true in WPF?

Tags:

c#

wpf

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.

It means

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

But, can this be inferred?

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


How I came to this conclusion

I did this

<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>

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

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.

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

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

like image 576
Nikhil Agrawal Avatar asked Sep 19 '12 07:09

Nikhil Agrawal


People also ask

What is trigger and how many types of triggers in WPF?

Basically, there are 3 types of triggers, they are: Property Trigger. Data Trigger. Event Trigger.

What are triggers in WPF?

The WPF styling and templating model enables you to specify triggers within your Style. Essentially, triggers are objects that enable you to apply changes when certain conditions (such as when a certain property value becomes true , or when an event occurs) are satisfied.


Video Answer


1 Answers

What you are telling, is correct.

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

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

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

"Remarks section"

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.

good luck :)

like image 160
Erti-Chris Eelmaa Avatar answered Sep 29 '22 18:09

Erti-Chris Eelmaa