选择其他选项卡的关闭按钮关闭当前选项卡
Selecting close button of other tab closes current tab
我正在创建一个动态创建新标签的应用程序。我为每个要关闭的选项卡添加了关闭按钮。
问题:
如果我打开了多个选项卡,并且我当前在 tab2 中,并且我 select 关闭了 tab4 的按钮,那么 tab2 将被关闭。
我还通过 RelayCommand 使用了我的 TabControl 的数据绑定。但这也有同样的问题。
请指出我哪里出错了以及我可以在我的代码中添加什么。
下面是我的代码。
XAML:
<TabControl x:Name="tabControl1" HorizontalAlignment="Stretch" MinHeight="50" Margin="0,0,0,0.2" Width="1215" ItemsSource="{Binding Titles, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="454" VerticalAlignment="Bottom">
<TabControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="21">
<TextBlock Text="{Binding Header}" />
<Button Name="BtnClose" Content="X" Cursor="Hand" DockPanel.Dock="Right" Focusable="False" FontFamily="Courier" FontSize="9" FontWeight="Bold" Margin="0,1,0,0" Padding="0" HorizontalAlignment="Right" VerticalContentAlignment="Bottom" Width="16" Height="16"
Command="{Binding DataContext.CloseTabCommand,RelativeSource={RelativeSource AncestorType={x:Type Window}}}" CommandParameter="{Binding ElementName=tabControl1,Path=SelectedItem}" />
<!--Click="BtnClose_Click"-->
</StackPanel>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<RichTextBox Margin="10" VerticalScrollBarVisibility="Visible">
<FlowDocument>
<Paragraph FontSize="12" FontFamily="Courier New">
<Run Text="{Binding Content}"></Run>
</Paragraph>
</FlowDocument>
</RichTextBox>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
我的 ViewModel class 我在其中创建了一个 class 选项卡项 "Item" 它将包含一个选项卡内容和 header .
public class MainWindowViewModel: INotifyPropertyChanged {
public MainWindowViewModel() {
Titles = new ObservableCollection < Item > ();
}
public class Item: INotifyPropertyChanged {
public string Header {
get;
set;
}
//public string Content { get; set; }
private string _content;
public static int _count = -1;
public int Count {
get {
return _count;
}
set {
_count = value; /* OnPropertyChanged(nameof(Count));*/
}
}
public string Content {
get {
return _content;
}
set {
_content = value;
OnPropertyChanged(nameof(Content));
}
}
public Item() {
_count++; //increase the count of tab. This will represent the index of the tab
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName) {
var handler = PropertyChanged;
handler ? .Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public ObservableCollection < Item > Titles {
get {
return _titles;
}
set {
_titles = value;
OnPropertyChanged("Titles");
}
}
static int tabs = 1;
private ObservableCollection < Item > _titles;
private RelayCommand < Item > _closeTabCommand;
public RelayCommand < Item > CloseTabCommand {
get {
return _closeTabCommand ?? (_closeTabCommand = new RelayCommand < Item > (
(t) => {
Titles.Remove(t);
}));
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName) {
var handler = PropertyChanged;
handler ? .Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
MainWindow.xaml.cs
private void BtnClose_Click(object sender, RoutedEventArgs e) {
//remove tab
MainWindowVMObj.RemoveTabItem(tabControl1.SelectedIndex);
}
关闭按钮的 CommandParameter
将选定的选项卡作为要关闭的选项卡传递。
一个简单的解决方法是绕过事件的当前源控件作为 CommandParameter
代替(在这种情况下它将是目标关闭按钮的父 TabItem):
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="21">
<TextBlock Text="{Binding Header}" />
<Button Name="BtnClose" Content="X" Cursor="Hand" DockPanel.Dock="Right" Focusable="False" FontFamily="Courier" FontSize="9" FontWeight="Bold" Margin="0,1,0,0" Padding="0" HorizontalAlignment="Right" VerticalContentAlignment="Bottom" Width="16" Height="16"
Command="{Binding DataContext.CloseTabCommand,RelativeSource={RelativeSource AncestorType={x:Type Window}}}" CommandParameter="{Binding}" />
</StackPanel>
</DataTemplate>
我正在创建一个动态创建新标签的应用程序。我为每个要关闭的选项卡添加了关闭按钮。
问题:
如果我打开了多个选项卡,并且我当前在 tab2 中,并且我 select 关闭了 tab4 的按钮,那么 tab2 将被关闭。 我还通过 RelayCommand 使用了我的 TabControl 的数据绑定。但这也有同样的问题。 请指出我哪里出错了以及我可以在我的代码中添加什么。
下面是我的代码。
XAML:
<TabControl x:Name="tabControl1" HorizontalAlignment="Stretch" MinHeight="50" Margin="0,0,0,0.2" Width="1215" ItemsSource="{Binding Titles, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="454" VerticalAlignment="Bottom">
<TabControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="21">
<TextBlock Text="{Binding Header}" />
<Button Name="BtnClose" Content="X" Cursor="Hand" DockPanel.Dock="Right" Focusable="False" FontFamily="Courier" FontSize="9" FontWeight="Bold" Margin="0,1,0,0" Padding="0" HorizontalAlignment="Right" VerticalContentAlignment="Bottom" Width="16" Height="16"
Command="{Binding DataContext.CloseTabCommand,RelativeSource={RelativeSource AncestorType={x:Type Window}}}" CommandParameter="{Binding ElementName=tabControl1,Path=SelectedItem}" />
<!--Click="BtnClose_Click"-->
</StackPanel>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<RichTextBox Margin="10" VerticalScrollBarVisibility="Visible">
<FlowDocument>
<Paragraph FontSize="12" FontFamily="Courier New">
<Run Text="{Binding Content}"></Run>
</Paragraph>
</FlowDocument>
</RichTextBox>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
我的 ViewModel class 我在其中创建了一个 class 选项卡项 "Item" 它将包含一个选项卡内容和 header .
public class MainWindowViewModel: INotifyPropertyChanged {
public MainWindowViewModel() {
Titles = new ObservableCollection < Item > ();
}
public class Item: INotifyPropertyChanged {
public string Header {
get;
set;
}
//public string Content { get; set; }
private string _content;
public static int _count = -1;
public int Count {
get {
return _count;
}
set {
_count = value; /* OnPropertyChanged(nameof(Count));*/
}
}
public string Content {
get {
return _content;
}
set {
_content = value;
OnPropertyChanged(nameof(Content));
}
}
public Item() {
_count++; //increase the count of tab. This will represent the index of the tab
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName) {
var handler = PropertyChanged;
handler ? .Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public ObservableCollection < Item > Titles {
get {
return _titles;
}
set {
_titles = value;
OnPropertyChanged("Titles");
}
}
static int tabs = 1;
private ObservableCollection < Item > _titles;
private RelayCommand < Item > _closeTabCommand;
public RelayCommand < Item > CloseTabCommand {
get {
return _closeTabCommand ?? (_closeTabCommand = new RelayCommand < Item > (
(t) => {
Titles.Remove(t);
}));
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName) {
var handler = PropertyChanged;
handler ? .Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
MainWindow.xaml.cs
private void BtnClose_Click(object sender, RoutedEventArgs e) {
//remove tab
MainWindowVMObj.RemoveTabItem(tabControl1.SelectedIndex);
}
关闭按钮的 CommandParameter
将选定的选项卡作为要关闭的选项卡传递。
一个简单的解决方法是绕过事件的当前源控件作为 CommandParameter
代替(在这种情况下它将是目标关闭按钮的父 TabItem):
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="21">
<TextBlock Text="{Binding Header}" />
<Button Name="BtnClose" Content="X" Cursor="Hand" DockPanel.Dock="Right" Focusable="False" FontFamily="Courier" FontSize="9" FontWeight="Bold" Margin="0,1,0,0" Padding="0" HorizontalAlignment="Right" VerticalContentAlignment="Bottom" Width="16" Height="16"
Command="{Binding DataContext.CloseTabCommand,RelativeSource={RelativeSource AncestorType={x:Type Window}}}" CommandParameter="{Binding}" />
</StackPanel>
</DataTemplate>