自定义选项卡项
我有一个绑定到我的 ViewModel 的 TabControl 我也想删除选项卡,但我看不到 tabitems 看到的默认值没有更改
<TabControl
Margin="5 5 5 0"
Grid.Row="1"
ItemsSource="{Binding Path=ListTabItem}"
SelectedIndex="{Binding Path=CurrentTabItemIndex}"
x:Name="Workspace" Background="White">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand
Command="{Binding ChangeTabCommand}"
CommandParameter="{Binding SelectedItem, ElementName=Workspace}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<!--Here is my tabHeader-->
<TabControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Header}" />
<Button Content="X" Command={Binding CloseTabCommand}
CommandParameter="{Binding SelectedItem, ElementName=Workspace}"/>
</StackPanel>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
编辑:
我得到了一种工作方式,但是当关闭选项卡不释放内存时我遇到了问题:
<Style x:Key="HeaderTabItem" TargetType="TabItem">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Button
Width="20"
Height="20"
Margin="-3 0 5 0"
Padding="2"
HorizontalAlignment="Left"
VerticalAlignment="Center"
BorderBrush="{x:Null}" Background="{x:Null}" Foreground="{x:Null}"
DataContext="{Binding DataContext, ElementName=Workspace}"
Command="{Binding Path=CloseTabCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=TabItem, AncestorLevel=1}}" Content="X"/>
<ContentControl
Grid.Column="1"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Content="{Binding Path=Header, RelativeSource={RelativeSource FindAncestor, AncestorType=TabItem, AncestorLevel=1}}" />
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
在我的视图模型中
CloseTabCommand = new RelayCommand<TabItem>(tab =>
{
if (null == tab) return;
ListTabItem.Remove(tab);
},
tab =>
{
return ListTabItem.IndexOf(tab) == CurrentTabItemIndex;
});