我有一个使用以下XAML的TabControl。
<TabControl x:Class="MyApp.Tab.TabContainer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MyApp.Tab"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
ItemsSource="{Binding}">
<TabControl.Resources>
<Style TargetType="TextBox">
<Setter Property="VerticalAlignment" Value="Stretch"></Setter>
<Setter Property="HorizontalAlignment" Value="Stretch"></Setter>
<Setter Property="AcceptsReturn" Value="True"></Setter>
<Setter Property="TextWrapping" Value="WrapWithOverflow"></Setter>
<Setter Property="MaxLines" Value="5000"></Setter>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"></Setter>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"></Setter>
</Style>
<SolidColorBrush x:Key="mouseOverColor" Color="Red"></SolidColorBrush>
<RadialGradientBrush x:Key="glowColor" Center="0.5,0.5" RadiusX="0.5" RadiusY="0.5" GradientOrigin="0.5,0.5">
<GradientStop Color="Red" Offset="0.5"></GradientStop>
</RadialGradientBrush>
<RadialGradientBrush x:Key="glowMask" Center="0.5,0.5" RadiusX="0.5" RadiusY="0.5" GradientOrigin="0.5,0.5">
<GradientStop Color="#80FFFFFF" Offset="0"></GradientStop>
<GradientStop Color="#80FFFFFF" Offset="0.6"></GradientStop>
<GradientStop Color="#00FFFFFF" Offset="1"></GradientStop>
</RadialGradientBrush>
<DataTemplate x:Key="NewTabHeaderTemplate" DataType="{x:Type local:AddNewTab}">
<DockPanel>
<TextBlock Name="TextBlock" VerticalAlignment="Center" Text="+" FontSize="18" MouseLeftButtonDown="TextBlock_OnMouseLeftButtonDown"></TextBlock>
</DockPanel>
</DataTemplate>
<DataTemplate x:Key="MapTabHeaderTemplate" DataType="local:ACTabItemTabItem">
<DockPanel>
<Button Name="btn_TabDelete" DockPanel.Dock="Right" Margin="5,0,0,0" Padding="0" Click="btn_TabDelete_Click" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Name}">
<Image Source="icon_grey_cross.gif" Height="9" Width="9" Margin="0,0,0,0"></Image>
<Button.Template>
<ControlTemplate TargetType="Button">
<Border x:Name="bdr_main" Margin="4" BorderThickness="0" >
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Margin="4,4,4,4" ContentSource="Content" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="bdr_main" Property="Background" Value="{StaticResource glowColor}"></Setter>
<Setter TargetName="bdr_main" Property="OpacityMask" Value="{StaticResource glowMask}"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
<TextBlock VerticalAlignment="Center" Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Header}"></TextBlock>
</DockPanel>
</DataTemplate>
</TabControl.Resources>
public class AddNewTab : TabItem {}
在构造函数中,我这样做是为了生成一个带有简单&#39; +&#39;的空白选项卡。在标题中。
_tabItems = new ObservableCollection<ACTabItem>();
try
{
AddNewTab tabAdd = new AddNewTab();
tabAdd.HeaderTemplate = this.FindResource("NewTabHeaderTemplate") as DataTemplate;
_tabItems.Add(tabAdd);
}
this.DataContext = _tabItems;
当用户点击&#39; +&#39;我创建一个新的TabItem并将其添加到Items集合中。然后我尝试将新标签放到前面。 (ImplementTab()只是创建一个新的TabItem,其中一些值由“td&#39;”的内容预设。
//code up here just collects some data from user to populate 'td'
TabItem newTab = ImplementTab(td);
if (newTab != null)
{
_tabItems.Add((ACTabItem)newTab);
newTab.Focus();
}
如果我观察this_SelectionChanged()事件处理程序,我看到Focus()调用确实导致SelectedIndex更改为新添加的选项卡的索引,但我也看到在新方法代码完成的方法之后(在Focus()之后),再次抛出SelectionChanged,但这次使用的是SelectedIndex的&#39; 0&#39; (带有&#39; +&#39;标题的原始空白标签页。)
单击选项卡只会抛出SelectionChanged一次,我会得到预期的结果。
我只是想让TabControl专注于新添加的TabItem ......我是怎么搞砸的?
答案 0 :(得分:0)
如果你做数据绑定,我觉得你不必从TabItem继承你的AddNewTab。它应该只是任何类,从TabItem继承它会让它变得混乱,因为TabControl将创建一个新的TabItem来包装这个数据项,所以你创建的选项卡上的所有方法如Focus()都没有效果。
要选择最后一个标签,只需设置SelectedIndex而不是调用Focus。
if (TabControl1.Items.Count > 0)
TabControl1.SelectedIndex = TabControl1.Items.Count - 1;