C# WPF可拖拽的TabControl
浏览导航
本文介绍利用第三方开源库 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
时候如流水,只能流去不流回!
点击《【浏览原文】》,本站另有更多技术类文章等着您哦!!!
保举浏览:三亚视窗