您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)使用WPF制作一個(gè)半圓形導(dǎo)航菜單,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來(lái)看看吧。
實(shí)現(xiàn)效果如下:
思路:
扇形自定義控件組合成半圓型菜單,再通過clip實(shí)現(xiàn)菜單的展開和折疊。
步驟:
1、扇形自定義控件CircularSectorControl
窗體布局xaml:
<Grid x:Name="mainGrid" MouseEnter="MainGrid_MouseEnter" MouseLeave="MainGrid_MouseLeave"> <Path x:Name="sectorPath" Data="M 200,200 0,200 A 200,200 0 0 1 58.6,58.6z" Fill="{Binding ElementName=sector, Path=BackgroundColor}"></Path> <Image Source="{Binding ElementName=sector, Path=DisplayImage}" Stretch="Fill" Width="35" Height="35" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="40,10"> <Image.RenderTransform> <RotateTransform Angle="-67.5"></RotateTransform> </Image.RenderTransform> </Image> </Grid>
交互邏輯:
public static readonly DependencyProperty DisplayImageProperty = DependencyProperty.Register("DisplayImage", typeof(ImageSource), typeof(CircularSectorControl), new PropertyMetadata(null)); public ImageSource DisplayImage { get { return (ImageSource)GetValue(DisplayImageProperty); } set { SetValue(DisplayImageProperty, value); } } public static readonly DependencyProperty BackgroundColorProperty = DependencyProperty.Register("BackgroundColor", typeof(SolidColorBrush), typeof(CircularSectorControl), new PropertyMetadata(null)); public SolidColorBrush BackgroundColor { get { return (SolidColorBrush)GetValue(BackgroundColorProperty); } set { SetValue(BackgroundColorProperty, value); } } public CircularSectorControl() { InitializeComponent(); } private void MainGrid_MouseEnter(object sender, MouseEventArgs e) { this.sectorPath.Fill = new SolidColorBrush(Color.FromRgb(246,111,111)); } private void MainGrid_MouseLeave(object sender, MouseEventArgs e) { this.sectorPath.Fill = BackgroundColor; }
2、半圓型菜單控件
窗體布局xaml:
<UserControl.Resources> <Storyboard x:Key="stbShow"> <DoubleAnimation Storyboard.TargetName="myEllipseGeometry" Storyboard.TargetProperty="RadiusX" Duration="0:0:0.5" From="0" To="200" FillBehavior="HoldEnd"/> <DoubleAnimation Storyboard.TargetName="myEllipseGeometry" Storyboard.TargetProperty="RadiusY" Duration="0:0:0.5" From="0" To="200" FillBehavior="HoldEnd" /> </Storyboard> <Storyboard x:Key="stbHide"> <DoubleAnimation Storyboard.TargetName="myEllipseGeometry" Storyboard.TargetProperty="RadiusX" Duration="0:0:0.5" From="200" To="0" FillBehavior="HoldEnd"/> <DoubleAnimation Storyboard.TargetName="myEllipseGeometry" Storyboard.TargetProperty="RadiusY" Duration="0:0:0.5" From="200" To="0" FillBehavior="HoldEnd" /> </Storyboard> </UserControl.Resources> <Canvas x:Name="mainCanvas" Cursor="Hand" ClipToBounds="True"> <Canvas x:Name="sectorCanvas"> <local:CircularSectorControl BackgroundColor="#F44E4E" DisplayImage="Images/1.png"></local:CircularSectorControl> <local:CircularSectorControl BackgroundColor="#F45757" DisplayImage="Images/2.png"> <local:CircularSectorControl.RenderTransform> <RotateTransform Angle="45" CenterX="200" CenterY="200"></RotateTransform> </local:CircularSectorControl.RenderTransform> </local:CircularSectorControl> <local:CircularSectorControl BackgroundColor="#F44E4E" DisplayImage="Images/3.png"> <local:CircularSectorControl.RenderTransform> <RotateTransform Angle="90" CenterX="200" CenterY="200"></RotateTransform> </local:CircularSectorControl.RenderTransform> </local:CircularSectorControl> <local:CircularSectorControl BackgroundColor="#F45757" DisplayImage="Images/4.png"> <local:CircularSectorControl.RenderTransform> <RotateTransform Angle="135" CenterX="200" CenterY="200"></RotateTransform> </local:CircularSectorControl.RenderTransform> </local:CircularSectorControl> </Canvas> <Path> <Path.Data> <EllipseGeometry x:Name="myEllipseGeometry" RadiusX="0" RadiusY="0" Center="200,200"></EllipseGeometry> </Path.Data> </Path> <Grid x:Name="bottomGrid" Canvas.Left="150" Canvas.Top="150" MouseLeftButtonDown="BottomGrid_MouseLeftButtonDown"> <Path Data="M 0,0 A 100,100 1 0 1 200,0z" Fill="White" Stretch="Fill" Width="100" Height="50"/> <TextBlock x:Name="bottomTB" Text="+" FontSize="38" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock> </Grid> </Canvas>
交互邏輯:
//委托 public delegate void EventHandle(bool isShow); public event EventHandle ShowClickEvent; private Storyboard storyboard = new Storyboard(); public RoundMenuControl() { InitializeComponent(); CompositionTarget.Rendering += UpdateEllipse; } private void UpdateEllipse(object sender, EventArgs e) { this.sectorCanvas.Clip = this.myEllipseGeometry; } private void BottomGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (this.bottomTB.Text == "+") { this.bottomTB.Text = "-"; Storyboard stbShow = (Storyboard)FindResource("stbShow"); stbShow.Begin(); ShowClickEvent?.Invoke(true); } else { this.bottomTB.Text = "+"; Storyboard stbHide = (Storyboard)FindResource("stbHide"); stbHide.Begin(); ShowClickEvent?.Invoke(false); } }
3、主窗體調(diào)用
窗體布局xaml:
<Window x:Class="RoundMenu.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:RoundMenu" Title="MainWindow" Width="650" Height="400" Background="#f6c06d" WindowStartupLocation="CenterScreen"> <Grid> <local:RoundMenuControl x:Name="roundMenu" Margin="125,170,100,0"></local:RoundMenuControl> </Grid> </Window>
交互邏輯:
public MainWindow() { InitializeComponent(); this.roundMenu.ShowClickEvent += RoundMenu_ShowClickEvent; } private void RoundMenu_ShowClickEvent(bool isShow) { if (isShow) this.Background = new SolidColorBrush(Color.FromRgb(255, 128, 79)); else this.Background = new SolidColorBrush(Color.FromRgb(246, 192, 109)); }
以上就是使用WPF制作一個(gè)半圓形導(dǎo)航菜單,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。