1+ // Copyright © 2017 Paddy Xu
2+ //
3+ // This file is part of QuickLook program.
4+ //
5+ // This program is free software: you can redistribute it and/or modify
6+ // it under the terms of the GNU General Public License as published by
7+ // the Free Software Foundation, either version 3 of the License, or
8+ // (at your option) any later version.
9+ //
10+ // This program is distributed in the hope that it will be useful,
11+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
12+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+ // GNU General Public License for more details.
14+ //
15+ // You should have received a copy of the GNU General Public License
16+ // along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+ using System ;
19+ using System . Collections . Generic ;
20+ using System . Windows ;
21+ using System . Windows . Markup ;
22+
23+ namespace QuickLook . Actions
24+ {
25+ [ ContentProperty ( "Actions" ) ]
26+ public class ConditionalEventTrigger : FrameworkContentElement
27+ {
28+ public static readonly DependencyProperty ConditionProperty =
29+ DependencyProperty . Register ( "Condition" , typeof ( bool ) , typeof ( ConditionalEventTrigger ) ) ;
30+
31+ public static readonly DependencyProperty TriggersProperty = DependencyProperty . RegisterAttached ( "Triggers" ,
32+ typeof ( ConditionalEventTriggerCollection ) , typeof ( ConditionalEventTrigger ) , new PropertyMetadata
33+ {
34+ PropertyChangedCallback = ( obj , e ) =>
35+ {
36+ // When "Triggers" is set, register handlers for each trigger in the list
37+ var element = ( FrameworkElement ) obj ;
38+ var triggers = ( List < ConditionalEventTrigger > ) e . NewValue ;
39+ foreach ( var trigger in triggers )
40+ element . AddHandler ( trigger . RoutedEvent , new RoutedEventHandler ( ( obj2 , e2 ) =>
41+ trigger . OnRoutedEvent ( element ) ) ) ;
42+ }
43+ } ) ;
44+
45+ private static readonly RoutedEvent TriggerActionsEvent = EventManager . RegisterRoutedEvent ( "" ,
46+ RoutingStrategy . Direct ,
47+ typeof ( EventHandler ) , typeof ( ConditionalEventTrigger ) ) ;
48+
49+ public ConditionalEventTrigger ( )
50+ {
51+ Actions = new List < TriggerAction > ( ) ;
52+ }
53+
54+ public RoutedEvent RoutedEvent { get ; set ; }
55+ public List < TriggerAction > Actions { get ; set ; }
56+
57+ // Condition
58+ public bool Condition
59+ {
60+ get => ( bool ) GetValue ( ConditionProperty ) ;
61+ set => SetValue ( ConditionProperty , value ) ;
62+ }
63+
64+ // "Triggers" attached property
65+ public static ConditionalEventTriggerCollection GetTriggers ( DependencyObject obj )
66+ {
67+ return ( ConditionalEventTriggerCollection ) obj . GetValue ( TriggersProperty ) ;
68+ }
69+
70+ public static void SetTriggers ( DependencyObject obj , ConditionalEventTriggerCollection value )
71+ {
72+ obj . SetValue ( TriggersProperty , value ) ;
73+ }
74+
75+ // When an event fires, check the condition and if it is true fire the actions
76+ private void OnRoutedEvent ( FrameworkElement element )
77+ {
78+ DataContext = element . DataContext ; // Allow data binding to access element properties
79+ if ( Condition )
80+ {
81+ // Construct an EventTrigger containing the actions, then trigger it
82+ var dummyTrigger = new EventTrigger { RoutedEvent = TriggerActionsEvent } ;
83+ foreach ( var action in Actions )
84+ dummyTrigger . Actions . Add ( action ) ;
85+
86+ element . Triggers . Add ( dummyTrigger ) ;
87+ try
88+ {
89+ element . RaiseEvent ( new RoutedEventArgs ( TriggerActionsEvent ) ) ;
90+ }
91+ finally
92+ {
93+ element . Triggers . Remove ( dummyTrigger ) ;
94+ }
95+ }
96+ }
97+ }
98+
99+ // Create collection type visible to XAML - since it is attached we cannot construct it in code
100+ public class ConditionalEventTriggerCollection : List < ConditionalEventTrigger >
101+ {
102+ }
103+ }
0 commit comments