@@ -32,8 +32,7 @@ private static void OnEditorChanged(DependencyObject obj, DependencyPropertyChan
3232 return ;
3333 }
3434
35- var bar = obj as TextToolbar ;
36- if ( bar != null )
35+ if ( obj is TextToolbar bar )
3736 {
3837 var oldEditor = args . OldValue as RichEditBox ;
3938 var newEditor = args . NewValue as RichEditBox ;
@@ -46,8 +45,15 @@ private static void OnEditorChanged(DependencyObject obj, DependencyPropertyChan
4645 if ( newEditor != null )
4746 {
4847 newEditor . AddHandler ( KeyDownEvent , bar . KeyEventHandler , handledEventsToo : true ) ;
49- bar . Formatter . SetModel ( bar ) ;
50- bar . DefaultButtons = bar . Formatter . DefaultButtons ;
48+ if ( bar . Formatter != null )
49+ {
50+ bar . Formatter . SetModel ( bar ) ;
51+ bar . DefaultButtons = bar . Formatter . DefaultButtons ;
52+ }
53+ else
54+ {
55+ bar . DefaultButtons = null ;
56+ }
5157 }
5258
5359 var editorArgs = new EditorChangedArgs
@@ -67,16 +73,19 @@ private static void OnEditorChanged(DependencyObject obj, DependencyPropertyChan
6773 /// <param name="args">Property Changed Args</param>
6874 private static void OnFormatterChanged ( DependencyObject obj , DependencyPropertyChangedEventArgs args )
6975 {
70- if ( obj is TextToolbar bar && bar . Formatter != null )
76+ if ( obj is TextToolbar bar )
7177 {
7278 if ( args . OldValue is Formatter formatter )
7379 {
7480 formatter . UnsetModel ( bar ) ;
7581 }
7682
77- bar . Formatter . SetModel ( bar ) ;
78- bar . DefaultButtons = bar . Formatter . DefaultButtons ;
79- bar . BuildBar ( ) ;
83+ if ( bar . Formatter != null )
84+ {
85+ bar . Formatter . SetModel ( bar ) ;
86+ bar . DefaultButtons = bar . Formatter . DefaultButtons ;
87+ bar . BuildBar ( ) ;
88+ }
8089 }
8190 }
8291
@@ -87,14 +96,11 @@ private static void OnFormatterChanged(DependencyObject obj, DependencyPropertyC
8796 /// <param name="args">Property Changed Args</param>
8897 private static void OnButtonMapChanged ( DependencyObject obj , DependencyPropertyChangedEventArgs args )
8998 {
90- var bar = obj as TextToolbar ;
91- if ( bar != null )
99+ if ( obj is TextToolbar bar )
92100 {
93- var oldSource = args . OldValue as ButtonMap ;
94- var newSource = args . NewValue as ButtonMap ;
95101 var root = bar . GetTemplateChild ( RootControl ) as CommandBar ;
96102
97- if ( oldSource != null )
103+ if ( args . OldValue is ButtonMap oldSource )
98104 {
99105 oldSource . CollectionChanged -= bar . OnButtonMapModified ;
100106
@@ -107,7 +113,7 @@ private static void OnButtonMapChanged(DependencyObject obj, DependencyPropertyC
107113 }
108114 }
109115
110- if ( newSource != null )
116+ if ( args . NewValue is ButtonMap newSource )
111117 {
112118 newSource . CollectionChanged += bar . OnButtonMapModified ;
113119
@@ -126,19 +132,16 @@ private static void OnButtonMapChanged(DependencyObject obj, DependencyPropertyC
126132 /// <param name="args">Property Changed Args</param>
127133 private static void OnDefaultButtonModificationsChanged ( DependencyObject obj , DependencyPropertyChangedEventArgs args )
128134 {
129- var bar = obj as TextToolbar ;
130- if ( bar != null )
135+ if ( obj is TextToolbar bar )
131136 {
132- var oldSource = args . OldValue as DefaultButtonModificationList ;
133- var newSource = args . NewValue as DefaultButtonModificationList ;
134137 var root = bar . GetTemplateChild ( RootControl ) as CommandBar ;
135138
136- if ( oldSource != null )
139+ if ( args . OldValue is DefaultButtonModificationList oldSource )
137140 {
138141 oldSource . CollectionChanged -= bar . OnDefaultButtonModificationListChanged ;
139142 }
140143
141- if ( newSource != null )
144+ if ( args . NewValue is DefaultButtonModificationList newSource )
142145 {
143146 newSource . CollectionChanged += bar . OnDefaultButtonModificationListChanged ;
144147
@@ -158,8 +161,7 @@ private static void OnDefaultButtonModificationsChanged(DependencyObject obj, De
158161 /// <param name="e">Collection Changed Args</param>
159162 private void OnButtonMapModified ( object sender , NotifyCollectionChangedEventArgs e )
160163 {
161- var root = GetTemplateChild ( RootControl ) as CommandBar ;
162- if ( root != null )
164+ if ( GetTemplateChild ( RootControl ) is CommandBar root )
163165 {
164166 switch ( e . Action )
165167 {
@@ -168,8 +170,7 @@ private void OnButtonMapModified(object sender, NotifyCollectionChangedEventArgs
168170 {
169171 AddToolbarItem ( item , root ) ;
170172
171- var button = item as ToolbarButton ;
172- if ( button != null )
173+ if ( item is ToolbarButton button )
173174 {
174175 button . PropertyChanged += ToolbarItemPropertyChanged ;
175176 }
@@ -182,8 +183,7 @@ private void OnButtonMapModified(object sender, NotifyCollectionChangedEventArgs
182183 {
183184 RemoveToolbarItem ( item ) ;
184185
185- var button = item as ToolbarButton ;
186- if ( button != null )
186+ if ( item is ToolbarButton button )
187187 {
188188 button . PropertyChanged -= ToolbarItemPropertyChanged ;
189189 }
@@ -225,8 +225,7 @@ private void OnDefaultButtonModificationListChanged(object sender, NotifyCollect
225225 /// <param name="e">Property Changed Event</param>
226226 private void ToolbarItemPropertyChanged ( object sender , System . ComponentModel . PropertyChangedEventArgs e )
227227 {
228- var root = GetTemplateChild ( RootControl ) as CommandBar ;
229- if ( root != null )
228+ if ( GetTemplateChild ( RootControl ) is CommandBar root )
230229 {
231230 if ( e . PropertyName == nameof ( IToolbarItem . Position ) )
232231 {
@@ -249,8 +248,7 @@ private void Editor_KeyDown(object sender, KeyRoutedEventArgs e)
249248
250249 LastKeyPress = e . Key ;
251250
252- var root = GetTemplateChild ( RootControl ) as CommandBar ;
253- if ( root != null )
251+ if ( GetTemplateChild ( RootControl ) is CommandBar root )
254252 {
255253 if ( ControlKeyDown && e . Key != VirtualKey . Control )
256254 {
0 commit comments