WPF实现半圆形导航菜单

作者:RunnerDNA 时间:2023-06-09 07:02:56 

本文实例为大家分享了WPF实现半圆形导航菜单的具体代码,供大家参考,具体内容如下

实现效果如下:

WPF实现半圆形导航菜单

思路:

扇形自定义控件组合成半圆型菜单,再通过clip实现菜单的展开和折叠。

步骤:

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、主窗体调用

窗体布局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));
}

来源:https://blog.csdn.net/dnazhd/article/details/108061673

标签:WPF,导航,菜单
0
投稿

猜你喜欢

  • c#的sortedlist使用方法

    2023-09-22 04:58:13
  • 浅谈Java异常的Exception e中的egetMessage()和toString()方法的区别

    2022-05-27 00:37:31
  • SpringBoot程序的打包与运行的实现

    2023-11-29 15:51:27
  • 详解如何让Spring MVC显示自定义的404 Not Found页面

    2023-12-12 15:48:47
  • C#如何访问共享文件夹或者磁盘

    2023-11-08 09:43:44
  • C#实现将汉字转化为2位大写的16进制Unicode的方法

    2022-03-11 21:45:07
  • Java非法字符: ‘\\ufeff‘问题及说明

    2023-02-01 09:06:07
  • Android语音声波控件 Android条形波控件

    2023-10-29 02:03:05
  • Java实现接口的枚举类示例

    2023-06-18 01:22:04
  • Android应用框架之应用启动过程详解

    2023-06-05 02:20:47
  • C#数据结构与算法揭秘二 线性结构

    2023-02-10 00:30:46
  • C#委托用法详解

    2023-06-04 22:46:43
  • Java下载远程服务器文件到本地(基于http协议和ssh2协议)

    2022-08-29 12:23:18
  • C# DataSet的内容写成XML时如何格式化字段数据

    2023-04-14 09:01:28
  • Qt for Android开发实例教程

    2023-06-27 10:00:39
  • SpringBoot项目@Async方法问题解决方案

    2023-11-12 03:55:26
  • SWT(JFace)体验之模拟BorderLayout布局

    2022-08-17 18:09:51
  • Android UI效果之绘图篇(四)

    2022-08-07 19:26:12
  • Android app启动时黑屏或者白屏的原因及解决办法

    2023-06-09 11:32:32
  • Java基础学习之关键字和变量数据类型的那些事

    2023-09-17 04:31:24
  • asp之家 软件编程 m.aspxhome.com