@@ -73,6 +73,9 @@ public TabSelectorPosition TabSelectorPosition
73
73
}
74
74
}
75
75
76
+ [ Category ( "Behavior" ) ]
77
+ public bool CloseableTabs { get ; set ; }
78
+
76
79
public TabControl ( string styleName = Stylesheet . DefaultStyleName ) : base ( new Grid ( ) )
77
80
{
78
81
HorizontalAlignment = HorizontalAlignment . Left ;
@@ -227,17 +230,48 @@ protected override void InsertItem(TabItem item, int index)
227
230
HorizontalAlignment = HorizontalAlignment . Stretch ,
228
231
VerticalAlignment = VerticalAlignment . Stretch ,
229
232
Height = item . Height ,
230
- Content = panel
233
+ Content = panel ,
234
+ ButtonsContainer = _gridButtons
231
235
} ;
232
236
233
237
button . ApplyButtonStyle ( TabControlStyle . TabItemStyle ) ;
234
238
235
239
button . Click += ButtonOnClick ;
236
240
237
- _gridButtons . Widgets . Insert ( index , button ) ;
238
-
239
241
item . Button = button ;
240
242
243
+ if ( ! CloseableTabs )
244
+ {
245
+ _gridButtons . Widgets . Insert ( index , button ) ;
246
+ } else
247
+ {
248
+ var topItemPanel = new HorizontalStackPanel ( ) ;
249
+ topItemPanel . Widgets . Add ( button ) ;
250
+ StackPanel . SetProportionType ( button , ProportionType . Fill ) ;
251
+
252
+ var closeButton = new Button
253
+ {
254
+ Content = new Image ( ) ,
255
+ HorizontalAlignment = HorizontalAlignment . Right
256
+ } ;
257
+
258
+ closeButton . Click += ( s , e ) => RemoveItem ( item ) ;
259
+
260
+ var style = TabControlStyle ;
261
+ if ( style . CloseButtonStyle != null )
262
+ {
263
+ closeButton . ApplyButtonStyle ( style . CloseButtonStyle ) ;
264
+ if ( style . CloseButtonStyle . ImageStyle != null )
265
+ {
266
+ var closeImage = ( Image ) closeButton . Content ;
267
+ closeImage . ApplyPressableImageStyle ( style . CloseButtonStyle . ImageStyle ) ;
268
+ }
269
+ }
270
+
271
+ topItemPanel . Widgets . Add ( closeButton ) ;
272
+ _gridButtons . Widgets . Insert ( index , topItemPanel ) ;
273
+ }
274
+
241
275
UpdateButtonsGrid ( ) ;
242
276
243
277
if ( Items . Count == 1 )
@@ -247,11 +281,32 @@ protected override void InsertItem(TabItem item, int index)
247
281
}
248
282
}
249
283
284
+ private int GetButtonIndex ( ListViewButton button )
285
+ {
286
+ var index = - 1 ;
287
+ for ( var i = 0 ; i < _gridButtons . Widgets . Count ; ++ i )
288
+ {
289
+ var widget = _gridButtons . Widgets [ i ] ;
290
+ if ( widget == button || widget . FindChild < ListViewButton > ( ) == button )
291
+ {
292
+ index = i ;
293
+ break ;
294
+ }
295
+ }
296
+
297
+ return index ;
298
+ }
299
+
250
300
protected override void RemoveItem ( TabItem item )
251
301
{
252
302
item . Changed -= ItemOnChanged ;
253
303
254
- var index = _gridButtons . Widgets . IndexOf ( item . Button ) ;
304
+ var index = GetButtonIndex ( item . Button ) ;
305
+ if ( index < 0 )
306
+ {
307
+ return ;
308
+ }
309
+
255
310
_gridButtons . Widgets . RemoveAt ( index ) ;
256
311
257
312
if ( SelectedItem == item )
@@ -287,8 +342,13 @@ protected override void Reset()
287
342
288
343
private void ButtonOnClick ( object sender , EventArgs eventArgs )
289
344
{
290
- var item = ( ListViewButton ) sender ;
291
- var index = _gridButtons . Widgets . IndexOf ( item ) ;
345
+ var button = ( ListViewButton ) sender ;
346
+ var index = GetButtonIndex ( button ) ;
347
+ if ( index < 0 )
348
+ {
349
+ return ;
350
+ }
351
+
292
352
SelectedIndex = index ;
293
353
}
294
354
0 commit comments