如何在WPF中使用关闭按钮关闭Tab?

uujelgoq  于 2023-05-01  发布在  其他
关注(0)|答案(2)|浏览(405)

我正在开发一个WPF应用程序,它在单击按钮时创建新的选项卡。这很好。我很难弄清楚如何有一个关闭按钮,就像一个X旁边的标签页标题和关闭选定的标签页?

主窗口。xaml

<Grid>
        <StackPanel Name="listConnections" Grid.Column="0" Background="#4682b4" Margin="0,0,0,-0.2" >
        </StackPanel>            
        <TabControl Name="tabConnections" Grid.Column="1" TabStripPlacement="Top" Margin="0,0,0.4,-0.2">
        </TabControl>
    </Grid>
</Window>

添加Tab方法,在按钮单击MainWindow时创建新的选项卡。xaml.cs

public void addTab(Connection connection)
{
    TabItem tab = new TabItem();
    tab.Header = connection.name;
    tabConnections.Items.Add(tab);
}

有没有一个简单的方法来做关闭按钮?

7gcisfzg

7gcisfzg1#

问题答案:

1.创建选项卡。
使用stack panel对齐文本框并水平关闭图像。检查下面的示例。
1.单击关闭时删除选项卡。
为了关闭一个标签,在代码后面创建一个事件处理程序来处理点击。在此事件处理程序中,您可以简单地用途:

tabConnections.Items.RemoveAt(tabConnections.SelectedIndex);

为什么要使用所选索引?这是因为当您单击选项卡时,该选项卡将成为选定选项卡。其中点击事件处理程序可以移除索引等于所选索引的选项卡。

示例:

在本例中,我为TabControl创建动态内容。通过使用您自己的UserControls作为内容。此外,此示例将在选项卡中提供关闭图像。所以首先创建一个Tab类和它后面的视图模式。

  • 标签 *
// This class will be the Tab int the TabControl
public class ActionTabItem
{
    // This will be the text in the tab control
    public string Header { get; set; }
    // This will be the content of the tab control It is a UserControl whits you need to create manualy
    public UserControl Content { get; set; }
}
  • 查看模式 *
/// view model for the TabControl To bind on
public class ActionTabViewModal
{
    // These Are the tabs that will be bound to the TabControl 
    public ObservableCollection<ActionTabItem> Tabs { get; set; }

    public ActionTabViewModal()
    {
        Tabs = new ObservableCollection<ActionTabItem>();
    }

    public void Populate()
    {
        // Add A tab to TabControl With a specific header and Content(UserControl)
        Tabs.Add(new ActionTabItem { Header = "UserControl 1", Content = new TestUserControl() });
        // Add A tab to TabControl With a specific header and Content(UserControl)
        Tabs.Add(new ActionTabItem { Header = "UserControl 2", Content = new TestUserControl() });
    }
}

现在我们需要创建xaml,它将选项卡项绑定到上面的视图模型。
1.将Action Tab item中的Header绑定到TabControl中的TextBlock
1.从关闭按钮图像给予图像控件一个路径
1.将ContentAction Tab item绑定到UserControl
1.使用堆栈面板的标题信息和关闭图像和水平对齐。

<Grid>
        <TabControl x:Name="actionTabs" DockPanel.Dock="Right" Background="White">
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Height="21" Width="100">
                        <TextBlock Width="80" Text="{Binding Header}"/>
                        <Image Source="PathToFile\close.png" Width="20" Height="20" MouseDown="Image_MouseDown"/>
                    </StackPanel>
                </DataTemplate>
            </TabControl.ItemTemplate>
            <TabControl.ContentTemplate>
                <DataTemplate>
                    <UserControl Height="800" Width="1220" Content="{Binding Content}" Margin="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
                </DataTemplate>
            </TabControl.ContentTemplate>
        </TabControl>
    </Grid>
  • 在背后的代码中 *
public partial class Window1 : Window
{
    private ActionTabViewModal vmd;

    public Window1()
    {
        InitializeComponent();
        // Initialize viewModel
        vmd  = new ActionTabViewModal();
        // Bind the xaml TabControl to view model tabs
        actionTabs.ItemsSource = vmd.Tabs;
        // Populate the view model tabs
        vmd.Populate();
    }

    private void Image_MouseDown(object sender, MouseButtonEventArgs e)
    { 
        // This event will be thrown when on a close image clicked
        vmd.Tabs.RemoveAt(actionTabs.SelectedIndex);
    }
}

结果:

z18hc3ub

z18hc3ub2#

这对我很有效
1.创建用户控件

<Grid >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Button Grid.Column="2" Content="X"  Height="19" OnClick="button_close_Click" HorizontalAlignment="Right" Margin="0,3,4,0" 
          VerticalAlignment="Top" Width="20" FontFamily="Courier" 
          FontWeight="Bold" 
          FontStretch="Normal"
          FontSize="14" Background="IndianRed"/>
        <Label Grid.ColumnSpan="2" Height="23" HorizontalAlignment="Left" 
          Margin="4,1,0,0" VerticalAlignment="Top" 
          FontFamily="Courier" FontSize="12" x:Name="NameLabel"/>
    </Grid>

2.用户控件类

public partial class CloseHeader : UserControl
    {
        MainWindow _shell;
        int _index;
        public CloseHeader(MainWindow shell, int index, string headerName)
        {
            InitializeComponent();
            _shell = shell;
            _index = index;
            NameLabel.Content = headerName;
        }

        private void button_close_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            _shell.CloseTab(_index);
        }
    }

1.在主窗口中

List<int> _tabs = new List<int>();
    int countIndex = 1;

    public void AddNewTab(HomeFunctions function)//HomeFunctions is an enum
    {
        int newIndex = countIndex++;
        _tabs.Add(newIndex);
        switch (function)
        {
            case HomeFunctions.Settings:
                tabControl.Items.Add(new TabItem { Content = new SettingsControl(this), Width = 100, Header = new CloseHeader(this, newIndex, HomeFunctions.Settings.ToString()) });
                break;
        }
        tabControl.SelectedIndex = newIndex;
    }

    public void CloseTab(int tabId)
    {
        tabControl.Items.RemoveAt(_tabs.IndexOf(tabId));//tabControl.Items.RemoveAt(_tabs.IndexOf(tabId) + 1); if you have a non closeable tab first like i do in mine
      _tabs.Remove(tabId);
    }

相关问题