閱讀導航
本文背景程式碼實現本文參考原始碼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 庫的樣式檔案:
<Window x:Class="TabMenu2.MainWindow" xmlns="/file/2020/01/18/20200118022508_13491.jpg.aspx xmlns:materialDesign="/file/2020/01/18/20200118022510_13492.jpg xmlns:d="/file/2020/01/18/20200118022511_13493.jpg.aspx xmlns:x="/file/2020/01/18/20200118022512_13494.jpg.aspx 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="/file/2020/01/18/20200118022513_13495.jpg </TextBlock> <WebBrowser Grid.Row="1" Margin="5" Source="/file/2020/01/18/20200118022514_13496.jpg </Grid> </TabItem> <TabItem> <TabItem.Header> <Image Source="/file/2020/01/18/20200118022515_13497.jpg.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.本文參考視訊一:C# WPF Material Design UI: Tab Menu,配套原始碼:TabMenu2。C# WPF開源控制元件庫《MaterialDesignInXAML》Dragablz-C# WPF可拖拽的TabControl控制元件4.原始碼
效果圖實現程式碼在文中已經全部給出,可直接Copy,按解決方案目錄組織程式碼檔案即可執行。
DragTabControl TabMenu2.exe Dragablz.dll MaterialDesignThemes.Wpf.dll MaterialDesignColors.dll除非註明,文章均由 Dotnet9 整理髮布,歡迎轉載。
時間如流水,只能流去不流回!
最新評論