天天看點

C# WPF可拖拽的TabControl

微信公衆号:Dotnet9,網站:Dotnet9,問題或建議:請網站留言,

如果對您有所幫助:歡迎贊賞。

C# WPF可拖拽的TabControl

閱讀導航

  1. 本文背景
  2. 代碼實作
  3. 本文參考
  4. 源碼

1. 本文背景

本文介紹使用第三方開源庫 Dragablz 實作可拖拽的 TabControl,本文代碼效果圖如下:

C# WPF可拖拽的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 的微信公衆号,本站會及時推送最新技術文章

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

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

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

時間如流水,隻能流去不流回。