我有一支精通的TabControl公司,试图积极补充:
public class CloseableTabControl : TabControl
{
protected override DependencyObject GetContainerForItemOverride()
{
return new CloseableTabItem();
}
}
public class CloseableTabItem : TabItem
{
public static readonly RoutedEvent CloseTabEvent =
EventManager.RegisterRoutedEvent("CloseTab", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(CloseableTabItem));
public event RoutedEventHandler CloseTab
{
add { AddHandler(CloseTabEvent, value); }
remove { RemoveHandler(CloseTabEvent, value); }
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var closeButton = Template.FindName("closeButton", this) as Button;
if (closeButton != null)
closeButton.Click += CloseButtonClick;
}
void CloseButtonClick(object sender, RoutedEventArgs e)
{
RaiseEvent(new RoutedEventArgs(CloseTabEvent, this));
}
}
并且XAML是:
<local:CloseableTabControl Grid.Column="2" Grid.Row="1" SelectedIndex="{Binding SelectedTabIndex}" ItemsSource="{Binding TabItems}">
<local:CloseableTabControl.Resources>
<DataTemplate DataType="{x:Type TestEditor:TestEditorViewModel}">
<TestEditor:TestEditorView/>
</DataTemplate>
<DataTemplate DataType="{x:Type TestParameterEditor:TestParameterEditorViewModel}">
<TestParameterEditor:TestParameterEditorView/>
</DataTemplate>
</local:CloseableTabControl.Resources>
<local:CloseableTabControl.ItemContainerStyle>
<Style TargetType="local:CloseableTabItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid>
<Border Name="Border" Background="{DynamicResource Brush2}" BorderBrush="{DynamicResource Brush5}" BorderThickness="1,1,1,1" CornerRadius="6,6,0,0" >
<DockPanel>
<ContentPresenter x:Name="ContentSite"
VerticalAlignment="Center"
HorizontalAlignment="Center"
ContentSource="Header"
Margin="12,2,12,2"/>
<Button Name="closeButton"
HorizontalAlignment="Center"
Margin="3,0,3,0"
VerticalAlignment="Center"
Width="16"
Height="16"
DockPanel.Dock="Right"
Style="{DynamicResource CloseableTabItemButtonStyle}"
ToolTip="Close Tab">
<Path Stretch="Fill" StrokeThickness="0.5" Stroke="#FF333333" Fill="#FF969696" Data="F1 M 2.28484e-007,1.33331L 1.33333,0L 4.00001,2.66669L 6.66667,6.10352e-005L 8,1.33331L 5.33334,4L 8,6.66669L 6.66667,8L 4,5.33331L 1.33333,8L 1.086e-007,6.66669L 2.66667,4L 2.28484e-007,1.33331 Z " HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Button>
</DockPanel>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource lightBrush}" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource Brush1}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Header">
<Setter.Value>
<StackPanel Orientation="Horizontal" ToolTip="{Binding Caption}">
<Image Height="18" Source="{Binding ImageName}"/>
<TextBlock Text="{Binding Title}" Margin="2,0,0,0"/>
</StackPanel>
</Setter.Value>
</Setter>
</Style>
</local:CloseableTabControl.ItemContainerStyle>
</local:CloseableTabControl>
当我只在这个窗子中添加一个字塔时,所有东西都是罚款的:
TabItems.Add(TestEditorViewModel);
but when I m adding more than one I m getting the mentioned exception.
I ve tried to follow the instructions here and separate the XAML to ItemTemplate
and ContentTemplate
but this caused a StackOverFlowException
(that is caused by the ContentPresenter
part of the template, I have no idea why).
Any ideas how should I approach this?
Thanks