读导航
本文介绍使用第三方开源库 Dragablz 实现可拖拽的 TabControl,本文代码效果图如下:
使用 .Net Framework 4.8 创建名为 “TabMenu2” 的WPF模板项目,添加三个Nuget库:MaterialDesignThemes、MaterialDesignColors 和 Dragablz,其中 TabControl 的拖拽功能是由 Dragablz 库实现的。
以下为三个库具体版本:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Dragablz" version="0.0.3.203" targetFramework="net45" />
<package id="MaterialDesignColors" version="1.2.3-ci948" targetFramework="net48" />
<package id="MaterialDesignThemes" version="3.1.0-ci948" targetFramework="net48" />
</packages>
解决方案主要文件目录组织结构:
注:站长尝试使用 .NET CORE 3.1 创建WPF项目,但 Dragablz 库暂时未提供 .NET CORE 的版本。想着自己编译 Dragablz 的 .NET CORE 版本,奈何功力不够,改了一些源码,最后放弃了。文中代码及文末给出的 Demo 运行程序需要在 .NET Framework 4.0 运行时环境下运行,想尝试编译 Dragablz 库的朋友可在文末给出的链接中下载编译。
文件【App.xaml】,在 StartupUri 中设置启动的视图【MainWindow.xaml】,并在【Application.Resources】节点增加 MaterialDesignThemes 和 Dragablz 库的样式文件:
<Application x:Class="TabMenu2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dragablz="clr-namespace:Dragablz;assembly=Dragablz"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- primary color -->
<ResourceDictionary>
<!-- include your primary palette -->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/MaterialDesignColor.purple.xaml" />
</ResourceDictionary.MergedDictionaries>
<!--
include three hues from the primary palette (and the associated forecolours).
Do not rename, keep in sequence; light to dark.
-->
<SolidColorBrush x:Key="PrimaryHueLightBrush" Color="{StaticResource Primary100}"/>
<SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="{StaticResource Primary100Foreground}"/>
<SolidColorBrush x:Key="PrimaryHueMidBrush" Color="{StaticResource Primary500}"/>
<SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="{StaticResource Primary500Foreground}"/>
<SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="{StaticResource Primary700}"/>
<SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="{StaticResource Primary700Foreground}"/>
</ResourceDictionary>
<!-- secondary colour -->
<ResourceDictionary>
<!-- include your secondary pallette -->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/MaterialDesignColor.purple.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- include a single secondary accent color (and the associated forecolour) -->
<SolidColorBrush x:Key="SecondaryAccentBrush" Color="{StaticResource Accent200}"/>
<SolidColorBrush x:Key="SecondaryAccentForegroundBrush" Color="{StaticResource Accent200Foreground}"/>
</ResourceDictionary>
<!-- Include the Dragablz Material Design style -->
<ResourceDictionary Source="pack://application:,,,/Dragablz;component/Themes/materialdesign.xaml"/>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- tell Dragablz tab control to use the Material Design theme -->
<Style TargetType="{x:Type dragablz:TabablzControl}" BasedOn="{StaticResource MaterialDesignTabablzControlStyle}" />
</ResourceDictionary>
</Application.Resources>
</Application>
文件【MainWindow.xaml】,引入 MaterialDesignThemes 和 Dragablz 库的命名空间,【dragablz:TabablzControl】为 Dragablz 库封装的 TabControl,使用方式和原生控件类似,单项标签依然使用 TabItem,使用起来很简单,源码如下:
<Window x:Class="TabMenu2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:dragablz="clr-namespace:Dragablz;assembly=Dragablz"
mc:Ignorable="d"
Height="600" Width="1080" ResizeMode="NoResize" WindowStartupLocation="CenterScreen"
MouseLeftButtonDown="Window_MouseLeftButtonDown" WindowStyle="None">
<Grid>
<Grid Height="60" VerticalAlignment="Top" Background="#FF9C27B0">
<TextBlock Text="Dotnet9.com:可拖拽TabControl" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="22" FontFamily="Champagne & Limousines" />
<Button HorizontalAlignment="Right" VerticalAlignment="Center" Background="{x:Null}" BorderBrush="{x:Null}" Click="Close_Click">
<materialDesign:PackIcon Kind="Close"/>
</Button>
</Grid>
<Grid Margin="0 60 0 0">
<dragablz:TabablzControl>
<dragablz:TabablzControl.InterTabController>
<dragablz:InterTabController/>
</dragablz:TabablzControl.InterTabController>
<TabItem Header="首页">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center">
<Run Text="欢迎访问Dotnet9的博客:"/>
<Hyperlink Click="ShowWeb_Click" Tag="https://dotnet9.com">https://dotnet9.com</Hyperlink>
</TextBlock>
<WebBrowser Grid.Row="1" Margin="5" Source="https://dotnet9.com"/>
</Grid>
</TabItem>
<TabItem Header="设计">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Text="为用户体验服务!" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<WebBrowser Grid.Row="1" Margin="5" Source="https://dotnet9.com"/>
</Grid>
</TabItem>
<TabItem Header="帮助">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center">
<Run Text="问答社区:"/>
<Hyperlink Click="ShowWeb_Click" Tag="https://dotnet9.com/questions-and-answers">https://dotnet9.com/questions-and-answers</Hyperlink>
</TextBlock>
<WebBrowser Grid.Row="1" Margin="5" Source="https://dotnet9.com/questions-and-answers"/>
</Grid>
</TabItem>
<TabItem>
<TabItem.Header>
<Image Source="https://img.dotnet9.com/logo.png"/>
</TabItem.Header>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30">
<Hyperlink Click="ShowWeb_Click" Tag="https://dotnet9.com">https://dotnet9.com</Hyperlink>
</TextBlock>
<WebBrowser Grid.Row="1" Margin="5" Source="https://dotnet9.com"/>
</Grid>
</TabItem>
</dragablz:TabablzControl>
</Grid>
</Grid>
</Window>
后台代码【MainWindow.xaml.cs】实现鼠标左键拖动窗体、右上角关闭窗体、超链接打开网站等功能:
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
private void Close_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void ShowWeb_Click(object sender, RoutedEventArgs e)
{
Process.Start((sender as Hyperlink).Tag.ToString());
}
效果图实现代码在文中已经全部给出,可直接Copy,按解决方案目录组织代码文件即可运行。
演示Demo(点击下载->DragTabControl,2.39 MB)目录结构:
除非注明,文章均由 Dotnet9 整理发布,欢迎转载。
转载请注明本文地址:https://dotnet9.com/7391.html
时间如流水,只能流去不流回!
点击《【阅读原文】》,本站还有更多技术类文章等着您哦!!!
下是一个使用C# WinForms中的TabControl控件的简单示例:
using System;
using System.Drawing;
using System.Windows.Forms;
public class MainForm : Form
{
private TabControl tabControl;
private TabPage tabPage1;
private TabPage tabPage2;
private TextBox textBox1;
private TextBox textBox2;
public MainForm()
{
// 创建主窗体
Text = "TabControl示例";
Size = new Size(400, 300);
// 创建TabControl控件
tabControl = new TabControl();
tabControl.Dock = DockStyle.Fill;
// 创建第一个选项卡页
tabPage1 = new TabPage();
tabPage1.Text = "选项卡1";
// 创建第一个选项卡页中的文本框
textBox1 = new TextBox();
textBox1.Multiline = true;
textBox1.Dock = DockStyle.Fill;
textBox1.Text = "选项卡1的内容";
// 将文本框添加到第一个选项卡页
tabPage1.Controls.Add(textBox1);
// 创建第二个选项卡页
tabPage2 = new TabPage();
tabPage2.Text = "选项卡2";
// 创建第二个选项卡页中的文本框
textBox2 = new TextBox();
textBox2.Multiline = true;
textBox2.Dock = DockStyle.Fill;
textBox2.Text = "选项卡2的内容";
// 将文本框添加到第二个选项卡页
tabPage2.Controls.Add(textBox2);
// 将选项卡页添加到TabControl控件
tabControl.TabPages.Add(tabPage1);
tabControl.TabPages.Add(tabPage2);
// 将TabControl控件添加到主窗体
Controls.Add(tabControl);
}
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}
}
在示例中,创建了一个名为MainForm的主窗体,并在窗体上添加了一个名为tabControl的TabControl控件。还创建了两个选项卡页tabPage1和tabPage2,并将它们分别添加到TabControl控件中。
在每个选项卡页中创建了一个文本框textBox1和textBox2,并将它们分别添加到对应的选项卡页中。
最后,将TabControl控件添加到主窗体的控件集合中。
运行此示例,您将看到一个具有TabControl控件和两个选项卡页的窗体。您可以切换选项卡来显示不同的内容。
WinForms 中,TabControl 控件用于创建标签页界面,允许用户在多个视图或页面之间切换。以下是如何使用 TabControl 控件的一些基本信息和示例代码:
以下是一个简单的示例,演示如何创建一个带有三个标签页的 TabControl 控件,并在每个标签页中添加控件:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
// 创建 TabControl 实例
TabControl tabControl1 = new TabControl();
// 设置 TabControl 的一些属性
tabControl1.Location = new Point(10, 10);
tabControl1.Size = new Size(300, 200);
// 创建第一个标签页
TabPage tabPage1 = new TabPage();
tabPage1.Text = "标签页 1";
tabControl1.TabPages.Add(tabPage1);
// 创建第二个标签页
TabPage tabPage2 = new TabPage();
tabPage2.Text = "标签页 2";
tabControl1.TabPages.Add(tabPage2);
// 创建第三个标签页
TabPage tabPage3 = new TabPage();
tabPage3.Text = "标签页 3";
tabControl1.TabPages.Add(tabPage3);
// 将 TabControl 添加到窗体上
this.Controls.Add(tabControl1);
// 为每个标签页添加控件
for (int i = 0; i < tabControl1.TabCount; i++)
{
TabPage currentTabPage = tabControl1.TabPages[i];
switch (i)
{
case 0:
Label label1 = new Label();
label1.Text = "这是标签页 1 的内容。";
currentTabPage.Controls.Add(label1);
break;
case 1:
Button button1 = new Button();
button1.Text = "这是标签页 2 的内容。";
currentTabPage.Controls.Add(button1);
break;
case 2:
TextBox textBox1 = new TextBox();
textBox1.Text = "这是标签页 3 的内容。";
currentTabPage.Controls.Add(textBox1);
break;
}
}
}
}
在这个示例中,我们创建了一个 TabControl 实例,并设置了其位置和大小。然后,我们创建了三个 TabPage 实例,并为每个实例设置了一个标题。接着,我们将这些 TabPage 实例添加到 TabControl 的 TabPages 集合中。最后,我们为每个标签页添加了不同类型的控件,并将它们添加到相应的 TabPage 中。
通过这些基本步骤,您可以在 WinForms 应用程序中使用 TabControl 控件来创建具有多个视图的用户界面,允许用户在不同的内容之间轻松切换。
*请认真填写需求信息,我们会在24小时内与您取得联系。