<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
WPF 實現抽屜選單
.NET40
;Visual Studio 2022
;1) DrawerMenu.cs 程式碼如下。
using System.Collections.Generic; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace WPFDevelopers.Controls { public class DrawerMenu : ContentControl { public new static readonly DependencyProperty ContentProperty = DependencyProperty.Register("Content", typeof(List<DrawerMenuItem>), typeof(DrawerMenu), new FrameworkPropertyMetadata(null)); public static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register("IsOpen", typeof(bool), typeof(DrawerMenu), new PropertyMetadata(true)); public static readonly DependencyProperty MenuIconColorProperty = DependencyProperty.Register("MenuIconColor", typeof(Brush), typeof(DrawerMenu), new PropertyMetadata(Brushes.White)); public static readonly DependencyProperty SelectionIndicatorColorProperty = DependencyProperty.Register("SelectionIndicatorColor", typeof(Brush), typeof(DrawerMenu), new PropertyMetadata(DrawingContextHelper.Brush)); public static readonly DependencyProperty MenuItemForegroundProperty = DependencyProperty.Register("MenuItemForeground", typeof(Brush), typeof(DrawerMenu), new PropertyMetadata(Brushes.Transparent)); static DrawerMenu() { DefaultStyleKeyProperty.OverrideMetadata(typeof(DrawerMenu), new FrameworkPropertyMetadata(typeof(DrawerMenu))); } public new List<DrawerMenuItem> Content { get => (List<DrawerMenuItem>)GetValue(ContentProperty); set => SetValue(ContentProperty, value); } public bool IsOpen { get => (bool)GetValue(IsOpenProperty); set => SetValue(IsOpenProperty, value); } public Brush MenuIconColor { get => (Brush)GetValue(MenuIconColorProperty); set => SetValue(MenuIconColorProperty, value); } public Brush SelectionIndicatorColor { get => (Brush)GetValue(SelectionIndicatorColorProperty); set => SetValue(SelectionIndicatorColorProperty, value); } public Brush MenuItemForeground { get => (Brush)GetValue(MenuItemForegroundProperty); set => SetValue(MenuItemForegroundProperty, value); } public override void BeginInit() { Content = new List<DrawerMenuItem>(); base.BeginInit(); } } }
2) DrawerMenuItem.cs 程式碼如下。
using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; namespace WPFDevelopers.Controls { public class DrawerMenuItem : ListBoxItem { public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(DrawerMenuItem), new PropertyMetadata(string.Empty)); public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(ImageSource), typeof(DrawerMenuItem), new PropertyMetadata(null)); public static readonly DependencyProperty SelectionIndicatorColorProperty = DependencyProperty.Register("SelectionIndicatorColor", typeof(Brush), typeof(DrawerMenuItem), new PropertyMetadata(DrawingContextHelper.Brush)); public static readonly DependencyProperty SelectionCommandProperty = DependencyProperty.Register("SelectionCommand", typeof(ICommand), typeof(DrawerMenuItem), new PropertyMetadata(null)); static DrawerMenuItem() { DefaultStyleKeyProperty.OverrideMetadata(typeof(DrawerMenuItem), new FrameworkPropertyMetadata(typeof(DrawerMenuItem))); } public string Text { get => (string)GetValue(TextProperty); set => SetValue(TextProperty, value); } public ImageSource Icon { get => (ImageSource)GetValue(IconProperty); set => SetValue(IconProperty, value); } public Brush SelectionIndicatorColor { get => (Brush)GetValue(SelectionIndicatorColorProperty); set => SetValue(SelectionIndicatorColorProperty, value); } public ICommand SelectionCommand { get => (ICommand)GetValue(SelectionCommandProperty); set => SetValue(SelectionCommandProperty, value); } } }
3) DrawerMenu.xaml 程式碼如下。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" xmlns:controls="clr-namespace:WPFDevelopers.Controls"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Basic/ControlBasic.xaml"/> </ResourceDictionary.MergedDictionaries> <Style x:Key="DrawerMenuToggleButton" TargetType="ToggleButton" BasedOn="{StaticResource ControlBasicStyle}"> <Setter Property="IsChecked" Value="False"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ToggleButton}"> <Grid Background="{TemplateBinding Background}"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> </Grid> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Opacity" Value="0.8" /> <Setter Property="Cursor" Value="Hand" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ToggleButton}"> <Border Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="1"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> </Border> </ControlTemplate> </Setter.Value> </Setter> </Trigger> </Style.Triggers> </Style> <Style x:Key="DrawerMenuListBox" TargetType="ListBox" BasedOn="{StaticResource ControlBasicStyle}"> <Setter Property="Background" Value="Transparent" /> <Setter Property="BorderBrush" Value="Transparent" /> <Setter Property="BorderThickness" Value="0"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <ScrollViewer> <ItemsPresenter Margin="0" /> </ScrollViewer> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="ButtonFocusVisual"> <Setter Property="Control.Template"> <Setter.Value> <ControlTemplate> <Border> <Rectangle Margin="2" StrokeThickness="1" Stroke="#60000000" StrokeDashArray="1 2"/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <!-- Fill Brushes --> <SolidColorBrush x:Key="NormalBrush" Color="Transparent" po:Freeze="True"/> <SolidColorBrush x:Key="DarkBrush" Color="#ddd" po:Freeze="True"/> <SolidColorBrush x:Key="PressedBrush" Color="#80FFFFFF" po:Freeze="True"/> <SolidColorBrush x:Key="DisabledForegroundBrush" Color="Transparent" po:Freeze="True"/> <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="Transparent" po:Freeze="True"/> <!-- Border Brushes --> <SolidColorBrush x:Key="NormalBorderBrush" Color="Transparent" po:Freeze="True"/> <SolidColorBrush x:Key="PressedBorderBrush" Color="Transparent" po:Freeze="True"/> <SolidColorBrush x:Key="DefaultedBorderBrush" Color="Transparent" po:Freeze="True"/> <SolidColorBrush x:Key="DisabledBorderBrush" Color="Transparent" po:Freeze="True"/> <Style x:Key="DrawerMenuItemButtonStyle" TargetType="Button" BasedOn="{StaticResource ControlBasicStyle}"> <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/> <Setter Property="MinHeight" Value="23"/> <Setter Property="MinWidth" Value="75"/> <Setter Property="Cursor" Value="Hand" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border x:Name="Border" CornerRadius="0" BorderThickness="0" Background="Transparent" BorderBrush="Transparent"> <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Stretch" RecognizesAccessKey="True"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsKeyboardFocused" Value="true"> <Setter TargetName="Border" Property="BorderBrush" Value="{DynamicResource DefaultedBorderBrush}" /> </Trigger> <Trigger Property="IsDefaulted" Value="true"> <Setter TargetName="Border" Property="BorderBrush" Value="{DynamicResource DefaultedBorderBrush}" /> </Trigger> <Trigger Property="IsMouseOver" Value="true"> <Setter TargetName="Border" Property="Background" Value="{DynamicResource DarkBrush}" /> </Trigger> <Trigger Property="IsPressed" Value="true"> <Setter TargetName="Border" Property="Background" Value="{DynamicResource PressedBrush}" /> <Setter TargetName="Border" Property="BorderBrush" Value="{DynamicResource PressedBorderBrush}" /> </Trigger> <Trigger Property="IsEnabled" Value="false"> <Setter TargetName="Border" Property="Background" Value="{DynamicResource DisabledBackgroundBrush}" /> <Setter TargetName="Border" Property="BorderBrush" Value="{DynamicResource DisabledBorderBrush}" /> <Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style TargetType="controls:DrawerMenuItem"> <Setter Property="HorizontalContentAlignment" Value="Stretch" /> <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:DrawerMenu}}, Path=MenuItemForeground}"/> <Setter Property="SelectionIndicatorColor" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:DrawerMenu}}, Path=SelectionIndicatorColor}"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="controls:DrawerMenuItem"> <Button x:Name="PART_Button" Height="44" Command="{TemplateBinding SelectionCommand}" ToolTip="{TemplateBinding Text}" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Style="{StaticResource DrawerMenuItemButtonStyle}"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="5"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Grid Grid.ColumnSpan="2"> <Grid Width="300"> <Grid.ColumnDefinitions> <ColumnDefinition Width="45"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Image Grid.Column="0" Source="{TemplateBinding Icon}" Margin="10,5,5,5"/> <TextBlock Text="{TemplateBinding Text}" Grid.Column="1" Margin="10,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="{StaticResource TitleFontSize}" Foreground="{TemplateBinding Foreground}" TextWrapping="Wrap"/> </Grid> </Grid> <Grid Name="PART_ItemSelectedIndicator" Grid.Column="0" Background="{TemplateBinding SelectionIndicatorColor}" Visibility="Collapsed" /> </Grid> </Button> <ControlTemplate.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter TargetName="PART_ItemSelectedIndicator" Property="Visibility" Value="Visible" /> </Trigger> <Trigger SourceName="PART_Button" Property="IsPressed" Value="True"> <Trigger.ExitActions> <BeginStoryboard> <Storyboard> <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsSelected"> <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="True" /> </BooleanAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </Trigger.ExitActions> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style TargetType="controls:DrawerMenu"> <Setter Property="Width" Value="50"/> <Setter Property="Visibility" Value="Visible"/> <Setter Property="IsOpen" Value="True"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="controls:DrawerMenu"> <Grid Background="{TemplateBinding Background}"> <ToggleButton HorizontalAlignment="Left" Background="#333" VerticalAlignment="Top" Height="40" Width="50" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:DrawerMenu}}, Path=IsOpen}" Style="{StaticResource DrawerMenuToggleButton}"> <Path HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Uniform" Width="20" Fill="{TemplateBinding MenuIconColor}" Data="{StaticResource PathMenu}"/> </ToggleButton> <ListBox ItemsSource="{TemplateBinding Content}" HorizontalAlignment="Left" Margin="0,40,0,0" VerticalAlignment="Top" Style="{StaticResource DrawerMenuListBox}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectedIndex="0"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsOpen" Value="False"> <Trigger.EnterActions> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetProperty="Width" To="180" Duration="0:0:0.2"/> </Storyboard> </BeginStoryboard> </Trigger.EnterActions> <Trigger.ExitActions> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetProperty="Width" To="50" Duration="0:0:0.2"/> </Storyboard> </BeginStoryboard> </Trigger.ExitActions> </Trigger> </Style.Triggers> </Style> </ResourceDictionary>
4) DrawerMenuExample.xaml 程式碼如下。
<UserControl x:Class="WPFDevelopers.Samples.ExampleViews.DrawerMenu.DrawerMenuExample" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews.DrawerMenu" xmlns:wpfdev="https://github.com/WPFDevelopersOrg/WPFDevelopers" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <Grid Background="#FF7B7BFF"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <wpfdev:DrawerMenu Background="#eee" SelectionIndicatorColor="{StaticResource PrimaryPressedSolidColorBrush}" MenuItemForeground="{StaticResource BlackSolidColorBrush}" HorizontalAlignment="Left"> <wpfdev:DrawerMenu.Content> <wpfdev:DrawerMenuItem Icon="pack://application:,,,/Images/CircularMenu/2.png" Text="主頁" SelectionCommand="{Binding HomeCommand,RelativeSource={RelativeSource AncestorType=local:DrawerMenuExample}}"/> <wpfdev:DrawerMenuItem Icon="pack://application:,,,/Images/CircularMenu/4.png" Text="Edge" SelectionCommand="{Binding EdgeCommand,RelativeSource={RelativeSource AncestorType=local:DrawerMenuExample}}"/> <wpfdev:DrawerMenuItem Icon="pack://application:,,,/Images/CircularMenu/1.png" Text="雲盤" SelectionCommand="{Binding CloudCommand,RelativeSource={RelativeSource AncestorType=local:DrawerMenuExample}}"/> <wpfdev:DrawerMenuItem Icon="pack://application:,,,/Images/CircularMenu/8.png" Text="郵件" SelectionCommand="{Binding MailCommand,RelativeSource={RelativeSource AncestorType=local:DrawerMenuExample}}"/> <wpfdev:DrawerMenuItem Icon="pack://application:,,,/Images/CircularMenu/6.png" Text="視訊" SelectionCommand="{Binding VideoCommand,RelativeSource={RelativeSource AncestorType=local:DrawerMenuExample}}"/> </wpfdev:DrawerMenu.Content> </wpfdev:DrawerMenu> <Frame Name="myFrame" Grid.Column="1" Margin="0,40,0,0" NavigationUIVisibility="Hidden"></Frame> </Grid> </UserControl>
5) DrawerMenuExample.xaml.cs 程式碼如下。
using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using WPFDevelopers.Samples.Helpers; namespace WPFDevelopers.Samples.ExampleViews.DrawerMenu { /// <summary> /// Win10MenuExample.xaml 的互動邏輯 /// </summary> public partial class DrawerMenuExample : UserControl { private List<Uri> _uriList = new List<Uri>() { new Uri("ExampleViews/DrawerMenu/HomePage.xaml",UriKind.Relative), new Uri("ExampleViews/DrawerMenu/EdgePage.xaml",UriKind.Relative), }; public DrawerMenuExample() { InitializeComponent(); myFrame.Navigate(_uriList[0]); } public ICommand HomeCommand => new RelayCommand(obj => { myFrame.Navigate(_uriList[0]); }); public ICommand EdgeCommand => new RelayCommand(obj => { myFrame.Navigate(_uriList[1]); }); public ICommand CloudCommand => new RelayCommand(obj => { WPFDevelopers.Minimal.Controls.MessageBox.Show("點選了雲盤","提示"); }); public ICommand MailCommand => new RelayCommand(obj => { WPFDevelopers.Minimal.Controls.MessageBox.Show("點選了郵件","提示"); }); public ICommand VideoCommand => new RelayCommand(obj => { WPFDevelopers.Minimal.Controls.MessageBox.Show("點選了視訊","提示"); }); } }
6) HomePage.xaml.cs 程式碼如下。
<Page x:Class="WPFDevelopers.Samples.ExampleViews.DrawerMenu.HomePage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews.DrawerMenu" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800" Title="HomePage" Background="{StaticResource PrimaryTextSolidColorBrush}"> <Grid> <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Width="400" TextAlignment="Center" TextWrapping="Wrap" Margin="0,0,0,40" FontSize="{StaticResource NormalFontSize}"> <Run Foreground="White">Home</Run> <Run Text="微信公眾號 WPFDevelopers" FontSize="40" Foreground="#A9CC32" FontWeight="Bold"></Run> <LineBreak/> <Hyperlink NavigateUri="https://github.com/WPFDevelopersOrg/WPFDevelopers.git" RequestNavigate="GithubHyperlink_RequestNavigate"> Github 原始碼</Hyperlink> <Run/> <Run/> <Run/> <Hyperlink NavigateUri="https://gitee.com/yanjinhua/WPFDevelopers.git" RequestNavigate="GiteeHyperlink_RequestNavigate"> 碼雲原始碼</Hyperlink> </TextBlock> </Grid> </Page>
7) EdgePage.xaml.cs 程式碼如下。
<Page x:Class="WPFDevelopers.Samples.ExampleViews.DrawerMenu.EdgePage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews.DrawerMenu" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800" Title="EdgePage" Background="{DynamicResource PrimaryPressedSolidColorBrush}"> <Grid> <StackPanel VerticalAlignment="Center" Margin="0,0,0,40"> <Image Source="pack://application:,,,/Images/CircularMenu/4.png" Stretch="Uniform" Width="50"/> <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" TextAlignment="Center" Foreground="White" Text="即將跳轉至Edge瀏覽器" FontSize="{StaticResource TitleFontSize}" Padding="0,10"> </TextBlock> </StackPanel> </Grid> </Page>
效果圖
到此這篇關於WPF實現抽屜選單效果的範例程式碼的文章就介紹到這了,更多相關WPF抽屜選單內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45