【发布时间】:2015-11-15 13:36:38
【问题描述】:
我正在尝试实现一件非常简单的事情。我在窗口上创建了一个选项卡控件(与 ObservableCollection 绑定)和一个按钮。当我单击一个按钮时,它会在此选项卡下创建一个带有小关闭按钮的新选项卡。当我按下这个关闭按钮时,一个按钮点击事件就会运行。
现在的问题是,当我按下关闭按钮时,它不会给出单击/选择的项目,因此我可以从 ObservableCollection 中删除关闭的选项卡。
以下是我的 Xaml 和按钮代码。
Xaml:
<TabControl x:Name="mytabcontrol" ItemsSource="{Binding TabItems}" Margin="10,66,36,10"> <!--SelectedItem="{Binding SelectedDetail, Mode=TwoWay}"-->
<TabControl.ItemTemplate>
<DataTemplate>
<!-- Tab item header: -->
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="tabarea" Text="{Binding Content}" />
<Button Content="X" Click="Button_Click" />
</StackPanel>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<!-- Tab item content goes here. -->
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
<Button Content="Click here" HorizontalAlignment="Left" Margin="10,27,0,0" VerticalAlignment="Top" Width="81" Click="Button_Click_1"/>
代码按钮。
private void Button_Click(object sender, RoutedEventArgs e)
{
//Here i need the clicked or selected item when prssing close button
TabItem ti = mytabcontrol.SelectedItem as TabItem; //Here i want to get the selected item when click on close button from tabcontrol, but it gives null.
MessageBox.Show(ti.Header.ToString());//Iam getting null here
}
请任何人在按下选项卡下的关闭按钮时帮助获取所选项目。在我附加的项目中,只需单击选项卡下的关闭按钮并查看单击按钮的行为,谢谢
【问题讨论】: