У меня есть CloseableTabControl с CloseableTabItems. Я пытаюсь динамически добавить:
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);
, но когда я добавляю более одной, я получаю упомянутое исключение.
Я пытался следовать инструкциям здесь и разделить XAML на ItemTemplate
и ContentTemplate
, но это вызвало StackOverFlowException
(что вызвано частью ContentPresenter
шаблона, я понятия не имею, почему).
Любые идеи, как следуетЯ подхожу к этому?
Спасибо