选择其他选项卡的关闭按钮将关闭当前选项卡

问题描述 投票:0回答:1

我正在创建一个应用,其中动态创建新标签页。我为每个要关闭的标签页添加了关闭按钮。

问题:

如果我打开了多个选项卡,并且我当前处于tab2中,并且选择了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类中创建一个选项卡项“ Item”,该选项卡将容纳选项卡的内容和标题。

 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);
 }
c# wpf mvvm tabcontrol
1个回答
0
投票

关闭按钮的CommandParameter正在将所选标签传递为要关闭的标签。

© www.soinside.com 2019 - 2024. All rights reserved.