AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.95k stars 2.25k forks source link

How to implement adding and closing tabitems #14394

Closed jinyuttt closed 9 months ago

jinyuttt commented 9 months ago

image

Can style be used to implement it?

stevemonaco commented 9 months ago

Traditionally, these aspects are expected to be implemented by the developer and not the framework as there are too many opinions on implementation.

TabItem.Header supports arbitrary content. So you can use a StackPanel, TextBlock, and Button for the closing part. If you're generating TabItems through a collection binding (ItemsSource), then you create DataTemplates for the header and content. Something like:

<TabControl ItemsSource="{Binding Items}">
    <TabControl.ItemTemplate>
    <DataTemplate x:DataType="vm:SomeViewModel">
        <StackPanel Orientation="Horizontal">
            <TextBlock MinWidth="50" Text="{Binding Description}" />
            <Button Command="{Binding Close}" />
        </StackPanel>
    </DataTemplate>
    </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
      <DataTemplate x:DataType="vm:SomeViewModel">
          <ContentControl Content="{Binding}" />
      </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

Since data can be put into the TabControl in multiple ways, it's up to you to implement the removal. If you're binding the ItemsSource to an ObservableCollection<T>, you simply remove the item from the collection.

AFAIK, there's no built-in support for an inline button to add items. That might require re-templating or clever use of TabItem.Header.