1
- using System . Collections . Specialized ;
1
+ using System ;
2
+ using System . Collections ;
3
+ using System . Collections . Generic ;
4
+ using System . Collections . Specialized ;
2
5
using System . ComponentModel ;
6
+ using System . Linq ;
3
7
using System . Windows ;
4
8
using System . Windows . Controls ;
5
9
@@ -157,13 +161,60 @@ protected override bool IsItemItsOwnContainerOverride(object item)
157
161
protected override void PrepareContainerForItemOverride ( DependencyObject element , object item )
158
162
{
159
163
base . PrepareContainerForItemOverride ( element , item ) ;
160
-
164
+ this . AttachVisibilityHandler ( element as WindowCommandsItem , item as UIElement ) ;
161
165
if ( ( Items . Count > 0 ) && ( ReferenceEquals ( item , Items [ Items . Count - 1 ] ) ) )
162
166
{
163
167
ResetSeparators ( false ) ;
164
168
}
165
169
}
166
170
171
+ protected override void ClearContainerForItemOverride ( DependencyObject element , object item )
172
+ {
173
+ base . ClearContainerForItemOverride ( element , item ) ;
174
+ this . DetachVisibilityHandler ( element as WindowCommandsItem ) ;
175
+ ResetSeparators ( false ) ;
176
+ }
177
+
178
+ private void AttachVisibilityHandler ( WindowCommandsItem container , UIElement item )
179
+ {
180
+ if ( container != null )
181
+ {
182
+ // hide the container, if there is no UIElement
183
+ if ( null == item )
184
+ {
185
+ container . Visibility = Visibility . Collapsed ;
186
+ return ;
187
+ }
188
+
189
+ container . Visibility = item . Visibility ;
190
+ var isVisibilityNotifier = new PropertyChangeNotifier ( item , UIElement . VisibilityProperty ) ;
191
+ isVisibilityNotifier . ValueChanged += VisibilityPropertyChanged ;
192
+ container . VisibilityPropertyChangeNotifier = isVisibilityNotifier ;
193
+ }
194
+ }
195
+
196
+ private void DetachVisibilityHandler ( WindowCommandsItem container )
197
+ {
198
+ if ( container != null )
199
+ {
200
+ container . VisibilityPropertyChangeNotifier = null ;
201
+ }
202
+ }
203
+
204
+ private void VisibilityPropertyChanged ( object sender , EventArgs e )
205
+ {
206
+ var item = sender as UIElement ;
207
+ if ( item != null )
208
+ {
209
+ var container = GetWindowCommandsItem ( item ) ;
210
+ if ( container != null )
211
+ {
212
+ container . Visibility = item . Visibility ;
213
+ ResetSeparators ( ) ;
214
+ }
215
+ }
216
+ }
217
+
167
218
protected override void OnItemsChanged ( NotifyCollectionChangedEventArgs e )
168
219
{
169
220
base . OnItemsChanged ( e ) ;
@@ -172,25 +223,43 @@ protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
172
223
173
224
private void ResetSeparators ( bool reset = true )
174
225
{
226
+ if ( Items . Count == 0 )
227
+ {
228
+ return ;
229
+ }
230
+
231
+ var windowCommandsItems = this . GetWindowCommandsItems ( ) . ToList ( ) ;
232
+
175
233
if ( reset )
176
234
{
177
- for ( var i = 0 ; i < Items . Count - 1 ; i ++ )
235
+ foreach ( var windowCommandsItem in windowCommandsItems )
178
236
{
179
- var container = ItemContainerGenerator . ContainerFromIndex ( i ) as WindowCommandsItem ;
180
- if ( container != null )
181
- {
182
- container . IsSeparatorVisible = ShowSeparators ;
183
- }
237
+ windowCommandsItem . IsSeparatorVisible = ShowSeparators ;
184
238
}
185
239
}
186
240
187
- var lastContainer = ItemContainerGenerator . ContainerFromIndex ( Items . Count - 1 ) as WindowCommandsItem ;
241
+ var lastContainer = windowCommandsItems . LastOrDefault ( i => i . IsVisible ) ;
188
242
if ( lastContainer != null )
189
243
{
190
244
lastContainer . IsSeparatorVisible = ShowSeparators && ShowLastSeparator ;
191
245
}
192
246
}
193
247
248
+ private WindowCommandsItem GetWindowCommandsItem ( object item )
249
+ {
250
+ var windowCommandsItem = item as WindowCommandsItem ;
251
+ if ( windowCommandsItem != null )
252
+ {
253
+ return windowCommandsItem ;
254
+ }
255
+ return ( WindowCommandsItem ) this . ItemContainerGenerator . ContainerFromItem ( item ) ;
256
+ }
257
+
258
+ private IEnumerable < WindowCommandsItem > GetWindowCommandsItems ( )
259
+ {
260
+ return ( from object item in ( IEnumerable ) this . Items select this . GetWindowCommandsItem ( item ) ) . Where ( i => i != null ) ;
261
+ }
262
+
194
263
private void WindowCommands_Loaded ( object sender , RoutedEventArgs e )
195
264
{
196
265
this . Loaded -= WindowCommands_Loaded ;
@@ -233,38 +302,19 @@ public class WindowCommandsItem : ContentControl
233
302
private const string PART_ContentPresenter = "PART_ContentPresenter" ;
234
303
private const string PART_Separator = "PART_Separator" ;
235
304
236
- private UIElement separator ;
237
- private bool isSeparatorVisible = true ;
238
-
239
- public bool IsSeparatorVisible
240
- {
241
- get { return isSeparatorVisible ; }
242
- set
243
- {
244
- if ( isSeparatorVisible == value )
245
- {
246
- return ;
247
- }
248
-
249
- isSeparatorVisible = value ;
250
- SetSeparatorVisibility ( ) ;
251
- }
252
- }
305
+ internal PropertyChangeNotifier VisibilityPropertyChangeNotifier { get ; set ; }
253
306
254
- private void SetSeparatorVisibility ( )
255
- {
256
- if ( separator != null )
257
- {
258
- separator . Visibility = IsSeparatorVisible ? Visibility . Visible : Visibility . Hidden ;
259
- }
260
- }
307
+ public static readonly DependencyProperty IsSeparatorVisibleProperty =
308
+ DependencyProperty . Register ( "IsSeparatorVisible" , typeof ( bool ) , typeof ( WindowCommandsItem ) ,
309
+ new FrameworkPropertyMetadata ( true , FrameworkPropertyMetadataOptions . Inherits | FrameworkPropertyMetadataOptions . AffectsArrange | FrameworkPropertyMetadataOptions . AffectsMeasure | FrameworkPropertyMetadataOptions . AffectsRender ) ) ;
261
310
262
- public override void OnApplyTemplate ( )
311
+ /// <summary>
312
+ /// Gets or sets the value indicating whether to show the separator.
313
+ /// </summary>
314
+ public bool IsSeparatorVisible
263
315
{
264
- base . OnApplyTemplate ( ) ;
265
-
266
- separator = Template . FindName ( PART_Separator , this ) as UIElement ;
267
- SetSeparatorVisibility ( ) ;
316
+ get { return ( bool ) GetValue ( IsSeparatorVisibleProperty ) ; }
317
+ set { SetValue ( IsSeparatorVisibleProperty , value ) ; }
268
318
}
269
319
}
270
320
}
0 commit comments