C# WPF可拖拽的TabControl

来源:https://www.cnblogs.com/Dotnet9-com/archive/2020/01/17/12207617.html
-Advertisement-
Play Games

微信公眾號: "Dotnet9" ,網站: "Dotnet9" ,問題或建議: "請網站留言" , 如果對您有所幫助: "歡迎贊賞" 。 C WPF可拖拽的TabControl 閱讀導航 1. 本文背景 2. 代碼實現 3. 本文參考 4. 源碼 1. 本文背景 本文介紹使用第三方開源庫 Draga ...


微信公眾號:Dotnet9,網站:Dotnet9,問題或建議:請網站留言
如果對您有所幫助:歡迎贊賞

C# WPF可拖拽的TabControl

閱讀導航

  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 &amp; 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

限時¥99,原價¥129

支付時輸入優惠口令:dotnet123

到手價¥89,僅限200人

.NET Core 的這些最佳實踐,你一定要學會!


△掃碼免費試看課程


除非註明,文章均由 Dotnet9 整理髮布,歡迎轉載。

轉載請註明本文地址:https://dotnet9.com/7391.html

歡迎掃描下方二維碼關註 Dotnet9 的微信公眾號,本站會及時推送最新技術文章

Dotnet9


時間如流水,只能流去不流回!

點擊《【閱讀原文】》,本站還有更多技術類文章等著您哦!!!

此刻順便為我點個《【再看】》可好?


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 前言 Golang 目前的主要應用領域還是後臺微服務,雖然在業務領域也有所應用但仍然是比較小衆的選擇。大多數的服務運行環境都是linux,而在windows中golang應用更少,而作者因爲特殊情況,不得已要在widows環境中用golang去寫本地代理服務。在我的使用場景中實時性要求非常高(視頻通 ...
  • import java.io.*; public class test13_6 { public static void main(String []args) throws Exception { FileOutputStream output=null; FileInputStream inpu ...
  • 原文:https://www.jianshu.com/p/e88d3f8151db JWT官網: https://jwt.io/ JWT(Java版)的github地址:https://github.com/jwtk/jjwt 什麼是JWT Json web token (JWT), 是為了在網路應 ...
  • 一、Unittest 單元測試框架,可用於自動化測試用力組織,執行,輸出結果 二、Unittest構成 1. Test Case 2. Test Suite 3. Test Fixture 4. Test Runner (圖片來源於網路) Test Case 一個測試用例是一個獨立的測試單元。它檢查 ...
  • 你可以訪問 "碼雲 樂優商城" 來獲取關於樂優商城的工程代碼。 你可以訪問 "百度雲 樂優優商城" 密碼:ppzy 來獲取關於樂優商城的資料。 一、介紹 樂優商城是一個全品類的電商購物網站(B2C),作為階段性學習工程。 二、軟體架構 三、前端技術選型 1. 基礎的HTML、CSS、JavaScri ...
  • 效果圖 首先是資料庫 /* Navicat MySQL Data Transfer Source Server : xm Source Server Version : 50553 Source Host : localhost:3306 Source Database : test Target ...
  • 一、概述 即時編譯器(Just In Time Compiler),也稱為 JIT 編譯器,它的主要工作是把熱點代碼編譯成與本地平臺相關的機器碼,併進行各種層次的優化,從而提高代碼執行的效率。 那麼什麼是熱點代碼呢?我們知道虛擬機通過解釋器(Interpreter)來執行位元組碼文件,當虛擬機發現某個 ...
  • 用已經搭建好 face_recognition,dlib 環境來進行人臉識別 未搭建好環境請參考:https://www.cnblogs.com/guihua-pingting/p/12201077.html 使用OpenCV 調用攝像頭 import face_recognition import ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...