【问题标题】:Selecting close button of other tab closes current tab选择其他选项卡的关闭按钮关闭当前选项卡
【发布时间】:2019-11-21 06:38:36
【问题描述】:

我正在创建一个动态创建新选项卡的应用程序。我为每个选项卡添加了关闭按钮以关闭。

问题:

如果我打开了多个选项卡,并且我当前在 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】:

    关闭按钮的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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多