1
+ using System ;
2
+ using System . ComponentModel ;
3
+ using System . Windows ;
4
+ using System . Windows . Controls ;
5
+ using System . Windows . Controls . Primitives ;
6
+ using System . Windows . Input ;
7
+ using MaterialDesignThemes . Wpf . Converters ;
8
+
9
+ namespace MaterialDesignThemes . Wpf
10
+ {
11
+ /// <summary>
12
+ /// Defines the content of a message within a <see cref="Snackbar2"/>. Primary content should be set via the
13
+ /// standard <see cref="SnackbarMessage.Content"/> property. Where an action is allowed, content
14
+ /// can be provided in <see cref="SnackbarMessage.ActionContent"/>. Standard button properties are
15
+ /// provided for actions, includiing <see cref="SnackbarMessage.ActionCommand"/>.
16
+ /// </summary>
17
+ [ TypeConverter ( typeof ( SnackbarMessageTypeConverter ) ) ]
18
+ [ TemplatePart ( Name = ActionButtonPartName , Type = typeof ( ButtonBase ) ) ]
19
+ public class SnackbarMessage : ContentControl
20
+ {
21
+ public const string ActionButtonPartName = "PART_ActionButton" ;
22
+ private Action _templateCleanupAction = ( ) => { } ;
23
+
24
+ static SnackbarMessage ( )
25
+ {
26
+ DefaultStyleKeyProperty . OverrideMetadata ( typeof ( SnackbarMessage ) , new FrameworkPropertyMetadata ( typeof ( SnackbarMessage ) ) ) ;
27
+ }
28
+
29
+ public static readonly DependencyProperty ActionCommandProperty = DependencyProperty . Register (
30
+ "ActionCommand" , typeof ( ICommand ) , typeof ( SnackbarMessage ) , new PropertyMetadata ( default ( ICommand ) ) ) ;
31
+
32
+ public ICommand ActionCommand
33
+ {
34
+ get { return ( ICommand ) GetValue ( ActionCommandProperty ) ; }
35
+ set { SetValue ( ActionCommandProperty , value ) ; }
36
+ }
37
+
38
+ public static readonly DependencyProperty ActionCommandParameterProperty = DependencyProperty . Register (
39
+ "ActionCommandParameter" , typeof ( object ) , typeof ( SnackbarMessage ) , new PropertyMetadata ( default ( object ) ) ) ;
40
+
41
+ public object ActionCommandParameter
42
+ {
43
+ get { return ( object ) GetValue ( ActionCommandParameterProperty ) ; }
44
+ set { SetValue ( ActionCommandParameterProperty , value ) ; }
45
+ }
46
+
47
+ public static readonly DependencyProperty ActionContentProperty = DependencyProperty . Register (
48
+ "ActionContent" , typeof ( object ) , typeof ( SnackbarMessage ) , new PropertyMetadata ( default ( object ) ) ) ;
49
+
50
+ public object ActionContent
51
+ {
52
+ get { return ( object ) GetValue ( ActionContentProperty ) ; }
53
+ set { SetValue ( ActionContentProperty , value ) ; }
54
+ }
55
+
56
+ public static readonly DependencyProperty ActionContentTemplateProperty = DependencyProperty . Register (
57
+ "ActionContentTemplate" , typeof ( DataTemplate ) , typeof ( SnackbarMessage ) , new PropertyMetadata ( default ( DataTemplate ) ) ) ;
58
+
59
+ public DataTemplate ActionContentTemplate
60
+ {
61
+ get { return ( DataTemplate ) GetValue ( ActionContentTemplateProperty ) ; }
62
+ set { SetValue ( ActionContentTemplateProperty , value ) ; }
63
+ }
64
+
65
+ public static readonly DependencyProperty ActionContentStringFormatProperty = DependencyProperty . Register (
66
+ "ActionContentStringFormat" , typeof ( string ) , typeof ( SnackbarMessage ) , new PropertyMetadata ( default ( string ) ) ) ;
67
+
68
+ public string ActionContentStringFormat
69
+ {
70
+ get { return ( string ) GetValue ( ActionContentStringFormatProperty ) ; }
71
+ set { SetValue ( ActionContentStringFormatProperty , value ) ; }
72
+ }
73
+
74
+ public static readonly DependencyProperty ActionContentTemplateSelectorProperty = DependencyProperty . Register (
75
+ "ActionContentTemplateSelector" , typeof ( DataTemplateSelector ) , typeof ( SnackbarMessage ) , new PropertyMetadata ( default ( DataTemplateSelector ) ) ) ;
76
+
77
+ public DataTemplateSelector ActionContentTemplateSelector
78
+ {
79
+ get { return ( DataTemplateSelector ) GetValue ( ActionContentTemplateSelectorProperty ) ; }
80
+ set { SetValue ( ActionContentTemplateSelectorProperty , value ) ; }
81
+ }
82
+
83
+ /// <summary>
84
+ /// Event correspond to left mouse button click on the Action button.
85
+ /// </summary>
86
+ public static readonly RoutedEvent ActionClickEvent = EventManager . RegisterRoutedEvent ( "ActionClick" ,
87
+ RoutingStrategy . Bubble , typeof ( RoutedEventHandler ) , typeof ( ButtonBase ) ) ;
88
+
89
+ /// <summary>
90
+ /// Add / Remove ActionClickEvent handler
91
+ /// </summary>
92
+ [ Category ( "Behavior" ) ]
93
+ public event RoutedEventHandler ActionClick { add { AddHandler ( ActionClickEvent , value ) ; } remove { RemoveHandler ( ActionClickEvent , value ) ; } }
94
+
95
+ protected virtual void OnActionClick ( )
96
+ {
97
+ var newEvent = new RoutedEventArgs ( ActionClickEvent , this ) ;
98
+ RaiseEvent ( newEvent ) ;
99
+ }
100
+
101
+ public override void OnApplyTemplate ( )
102
+ {
103
+ _templateCleanupAction ( ) ;
104
+
105
+ var buttonBase = GetTemplateChild ( ActionButtonPartName ) as ButtonBase ;
106
+ if ( buttonBase != null )
107
+ {
108
+ buttonBase . Click += ButtonBaseOnClick ;
109
+
110
+ _templateCleanupAction = ( ) => buttonBase . Click -= ButtonBaseOnClick ;
111
+ }
112
+ else
113
+ _templateCleanupAction = ( ) => { } ;
114
+
115
+ base . OnApplyTemplate ( ) ;
116
+ }
117
+
118
+ private void ButtonBaseOnClick ( object sender , RoutedEventArgs routedEventArgs )
119
+ {
120
+ OnActionClick ( ) ;
121
+ }
122
+ }
123
+ }
0 commit comments