Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can't use EventTrigger in this way. WPF's RoutedEventHandler that invokes EventTriggers doesn't provide any mechanism for making the trigger conditional, and you can't fix this by subclassing TriggerAction because there is no protected Invoke() or Execute() action to override.</p> <p>However this can be done quite easily using a custom class. Here's how it would be used:</p> <pre><code>&lt;Border&gt; &lt;my:ConditionalEventTrigger.Triggers&gt; &lt;my:ConditionalEventTriggerCollection&gt; &lt;my:ConditionalEventTrigger RoutedEvent="Mouse.MouseLeave" Condition="{Binding IsChecked, ElementName=checkbox}"&gt; &lt;BeginStoryboard Name="out_BeginStoryboard" Storyboard="{StaticResource out}" /&gt; &lt;RemoveStoryboard BeginStoryboardName="over_BeginStoryboard" /&gt; &lt;/my:ConditionalEventTrigger&gt; &lt;/my:ConditionalEventTriggerCollection&gt; &lt;/my:ConditionalEventTrigger.Triggers&gt; ... </code></pre> <p>And here's how it would be implemented:</p> <pre><code>[ContentProperty("Actions")] public class ConditionalEventTrigger : FrameworkContentElement { public RoutedEvent RoutedEvent { get; set; } public List&lt;TriggerAction&gt; Actions { get; set; } // Condition public bool Condition { get { return (bool)GetValue(ConditionProperty); } set { SetValue(ConditionProperty, value); } } public static readonly DependencyProperty ConditionProperty = DependencyProperty.Register("Condition", typeof(bool), typeof(ConditionalEventTrigger)); // "Triggers" attached property public static ConditionalEventTriggerCollection GetTriggers(DependencyObject obj) { return (ConditionalEventTriggerCollection)obj.GetValue(TriggersProperty); } public static void SetTriggers(DependencyObject obj, ConditionalEventTriggerCollection value) { obj.SetValue(TriggersProperty, value); } public static readonly DependencyProperty TriggersProperty = DependencyProperty.RegisterAttached("Triggers", typeof(ConditionalEventTriggerCollection), typeof(ConditionalEventTrigger), new PropertyMetadata { PropertyChangedCallback = (obj, e) =&gt; { // When "Triggers" is set, register handlers for each trigger in the list var element = (FrameworkElement)obj; var triggers = (List&lt;ConditionalEventTrigger&gt;)e.NewValue; foreach(var trigger in triggers) element.AddHandler(trigger.RoutedEvent, new RoutedEventHandler((obj2, e2) =&gt; trigger.OnRoutedEvent(element))); } }); public ConditionalEventTrigger() { Actions = new List&lt;TriggerAction&gt;(); } // When an event fires, check the condition and if it is true fire the actions void OnRoutedEvent(FrameworkElement element) { DataContext = element.DataContext; // Allow data binding to access element properties if(Condition) { // Construct an EventTrigger containing the actions, then trigger it var dummyTrigger = new EventTrigger { RoutedEvent = _triggerActionsEvent }; foreach(var action in Actions) dummyTrigger.Actions.Add(action); element.Triggers.Add(dummyTrigger); try { element.RaiseEvent(new RoutedEventArgs(_triggerActionsEvent)); } finally { element.Triggers.Remove(dummyTrigger); } } } static RoutedEvent _triggerActionsEvent = EventManager.RegisterRoutedEvent("", RoutingStrategy.Direct, typeof(EventHandler), typeof(ConditionalEventTrigger)); } // Create collection type visible to XAML - since it is attached we cannot construct it in code public class ConditionalEventTriggerCollection : List&lt;ConditionalEventTrigger&gt; {} </code></pre> <p>Enjoy!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload