@@ -73,6 +73,9 @@ public TabSelectorPosition TabSelectorPosition
7373 }
7474 }
7575
76+ [ Category ( "Behavior" ) ]
77+ public bool CloseableTabs { get ; set ; }
78+
7679 public TabControl ( string styleName = Stylesheet . DefaultStyleName ) : base ( new Grid ( ) )
7780 {
7881 HorizontalAlignment = HorizontalAlignment . Left ;
@@ -227,17 +230,48 @@ protected override void InsertItem(TabItem item, int index)
227230 HorizontalAlignment = HorizontalAlignment . Stretch ,
228231 VerticalAlignment = VerticalAlignment . Stretch ,
229232 Height = item . Height ,
230- Content = panel
233+ Content = panel ,
234+ ButtonsContainer = _gridButtons
231235 } ;
232236
233237 button . ApplyButtonStyle ( TabControlStyle . TabItemStyle ) ;
234238
235239 button . Click += ButtonOnClick ;
236240
237- _gridButtons . Widgets . Insert ( index , button ) ;
238-
239241 item . Button = button ;
240242
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+
241275 UpdateButtonsGrid ( ) ;
242276
243277 if ( Items . Count == 1 )
@@ -247,11 +281,32 @@ protected override void InsertItem(TabItem item, int index)
247281 }
248282 }
249283
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+
250300 protected override void RemoveItem ( TabItem item )
251301 {
252302 item . Changed -= ItemOnChanged ;
253303
254- var index = _gridButtons . Widgets . IndexOf ( item . Button ) ;
304+ var index = GetButtonIndex ( item . Button ) ;
305+ if ( index < 0 )
306+ {
307+ return ;
308+ }
309+
255310 _gridButtons . Widgets . RemoveAt ( index ) ;
256311
257312 if ( SelectedItem == item )
@@ -287,8 +342,13 @@ protected override void Reset()
287342
288343 private void ButtonOnClick ( object sender , EventArgs eventArgs )
289344 {
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+
292352 SelectedIndex = index ;
293353 }
294354
0 commit comments