新疆都市网_打造全面的专业的企业资讯门户!
您当前的位置 : 新疆都市网  >  科技
C# WPF可拖拽的TabControl
2020-03-29 10:05:45 来源:互联网 阅读:-

阅读导航

  1. 本文背景
  2. 代码实现
  3. 本文参考
  4. 源码

1. 本文背景

本文介绍使用第三方开源库 Dragablz 实现可拖拽的 TabControl,本文代码效果图如下:



2. 代码实现

使用 .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>

解决方案主要文件目录组织结构:

  • TabMenu2
  • App.xaml
  • MainWindow.xaml
  • MainWIndow.xaml.cs

注:站长尝试使用 .NET CORE 3.1 创建WPF项目,但 Dragablz 库暂时未提供 .NET CORE 的版本。想着自己编译 Dragablz 的 .NET CORE 版本,奈何功力不够,改了一些源码,最后放弃了。文中代码及文末给出的 Demo 运行程序需要在 .NET Framework 4.0 运行时环境下运行,想尝试编译 Dragablz 库的朋友可在文末给出的链接中下载编译。

2.1 引入样式

文件【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>

2.2 演示窗体布局

文件【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());}

3.本文参考

  1. 视频一:C# WPF Material Design UI: Tab Menu,配套源码:TabMenu2。
  2. C# WPF开源控件库《MaterialDesignInXAML》
  3. Dragablz-C# WPF可拖拽的TabControl控件

4.源码

效果图实现代码在文中已经全部给出,可直接Copy,按解决方案目录组织代码文件即可运行。

演示Demo(点击下载->DragTabControl,2.39 MB)目录结构:

  • DragTabControl
  • TabMenu2.exe
  • Dragablz.dll
  • MaterialDesignThemes.Wpf.dll
  • MaterialDesignColors.dll


除非注明,文章均由 Dotnet9 整理发布,欢迎转载。

转载请注明本文地址:https://dotnet9.com/7391.html


时间如流水,只能流去不流回!

点击《【阅读原文】》,本站还有更多技术类文章等着您哦!!!

推荐阅读:苹果x跟xr哪个好

频道推荐
  • 旗舰手机“花里胡哨”?为何懂手机的人都选千元机
    旗舰手机“花里胡哨”?为何懂手机的人都选千元

    一直以来,旗舰机都是手机厂牌的门面担当,不仅拥有最前沿的黑科技,还有各种不俗的硬核配置,不过在真实的市场中,他们并不是主角,千元机才是最受用户选购的机型。所谓“...

    2019-10-09
  • 四年前的苹果6s,相当于现在什么级别的国产手机?
    四年前的苹果6s,相当于现在什么级别的国产手

    尽管不想承认,目前苹果手机的性能、配置,依然很顶尖,从处理器的跑分,就可见一斑,同一代处理器,华为的麒麟990,以及高通的骁龙855 plus,单核跑分都是12...

    2019-10-09
  • 荣耀迎来5G概念机?麒麟990+6400W像素+2K曲面屏,令人感到惊艳
    荣耀迎来5G概念机?麒麟990+6400W像

    摘要:华为在前段时间发布的mate30系列之所以能够获得大量消费的者的喜爱,因为华为不但使用了5G技术,在拍照方面也具有优秀的性能,而华为旗下的荣耀手机最近也有...

    2019-10-09
  • iPhone 11陷刮擦门:屏幕脆 塑料钥匙扣就能制造划痕
    iPhone 11陷刮擦门:屏幕脆 塑料钥匙

    近日,有用户反映iphone11屏幕划痕问题,部分用户将iPhone 11单独放在一个的口袋中,也没有钥匙或其他物品,但仍然出现划痕。经过媒体的实际测试,塑料钥...

    2019-10-09
  • 向安卓致敬!iPhone 12装上呼吸灯,机身比安卓还花里胡哨
    向安卓致敬!iPhone 12装上呼吸灯,机

    可以说能叫做花里胡哨的手机,一定只能在安卓手机中找到,在苹果手机中是无法找到的。因为安卓手机比较注重外观色彩,所以我们可以看到有很多颜色比较怪异的手机,而苹果手...

    2019-10-09
  • 微博明星爱用什么手机?9月明星势力榜前100名 只有16人用国产机
    微博明星爱用什么手机?9月明星势力榜前100

    这两天有些无聊,自己简单统计了一下微博9月明星势力榜前100位明星所使用的的手机。纯手动统计,可能略有出入。根据统计显示,100位明星的微博小尾巴中,iPhon...

    2019-10-09
  • 6000元谁主沉浮 华为Mate系列这成绩打败苹果
    6000元谁主沉浮 华为Mate系列这成绩打

    都说“果粉”眼里只有苹果,其他的手机根本看不上眼,再更换新机的时候还是会选iPhone。那其他品牌的“品牌忠诚度”如何呢?最近前魅族科技原高级副总裁李楠发文表示...

    2019-10-09
  • 首尔市2022年实现免费WiFi全覆盖
    首尔市2022年实现免费WiFi全覆盖

    人民网讯 据韩联社报道,韩国首尔市政府计划到2022年,免费公共WiFi将覆盖首尔市全境,接入公共物联网的共享停车、智能路灯、防走失等服务也将推广。首尔市政府7...

    2019-10-09