91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

WPF如何實現環形菜單

發布時間:2022-07-30 09:28:44 來源:億速云 閱讀:174 作者:iii 欄目:開發技術

這篇文章主要介紹“WPF如何實現環形菜單”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“WPF如何實現環形菜單”文章能幫助大家解決問題。

實現代碼

1.CircularMenuItemCustomControl.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WpfCircularMenu
{

    [TemplatePart(Name = RotateTransformTemplateName, Type = typeof(RotateTransform))]
    public class CircularMenuItemCustomControl : Control
    {
        private static readonly Type _typeofSelf = typeof(CircularMenuItemCustomControl);
        private const string RotateTransformTemplateName = "PART_RotateTransform";
        private RotateTransform _angleRotateTransform;
        public double Angle
        {
            get { return (double)GetValue(AngleProperty); }
            set { SetValue(AngleProperty, value); }
        }

        public static readonly DependencyProperty AngleProperty =
            DependencyProperty.Register("Angle", typeof(double), typeof(CircularMenuItemCustomControl), new UIPropertyMetadata(OnAngleChanged));

        private static void OnAngleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CircularMenuItemCustomControl control = (CircularMenuItemCustomControl)d;
            control.UpdateAngle();
        }
        void UpdateAngle()
        {
            if (_angleRotateTransform == null) return;
            _angleRotateTransform.Angle = Angle;
        }
        public string MenuTxt
        {
            get { return (string)GetValue(MenuTxtProperty); }
            set { SetValue(MenuTxtProperty, value); }
        }

        public static readonly DependencyProperty MenuTxtProperty =
            DependencyProperty.Register("MenuTxt", typeof(string), typeof(CircularMenuItemCustomControl), new PropertyMetadata(string.Empty));



        public Brush BackgroundColor
        {
            get { return (Brush)GetValue(BackgroundColorProperty); }
            set { SetValue(BackgroundColorProperty, value); }
        }
        public static readonly DependencyProperty BackgroundColorProperty =
           DependencyProperty.Register("BackgroundColor", typeof(Brush), typeof(CircularMenuItemCustomControl), new PropertyMetadata(null));

        public ImageSource IconImage
        {
            get { return (ImageSource)GetValue(IconImageProperty); }
            set { SetValue(IconImageProperty, value); }
        }
        public static readonly DependencyProperty IconImageProperty = 
            DependencyProperty.Register("IconImage", typeof(ImageSource), typeof(CircularMenuItemCustomControl), new PropertyMetadata(null));
       
        static CircularMenuItemCustomControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(_typeofSelf, new FrameworkPropertyMetadata(_typeofSelf));
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _angleRotateTransform = GetTemplateChild(RotateTransformTemplateName) as RotateTransform;
            UpdateAngle();
        }
       

    }
}

2.CircularMenuItemCustomControlStyle.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WpfCircularMenu">
    <Style TargetType="{x:Type local:CircularMenuItemCustomControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:CircularMenuItemCustomControl">
                    <Grid VerticalAlignment="Center">
                        <Grid.RenderTransform>
                            <RotateTransform x:Name="PART_RotateTransform" Angle="{TemplateBinding Angle}" CenterX="200" CenterY="200"></RotateTransform>
                        </Grid.RenderTransform>
                        <Path x:Name="PART_Path" Data="M 200,200 0,200 A 200,200 0 0 1 58.6,58.6z" 
                                  Fill="{TemplateBinding BackgroundColor}" VerticalAlignment="Center"/>
                        <Image Source="{TemplateBinding IconImage}" RenderTransformOrigin="0.5,0.5"
                                   Margin="60,70,0,0" 
                                   HorizontalAlignment="Left" 
                                   VerticalAlignment="Center" 
                                   Width="40" Height="40" >
                            <Image.RenderTransform>
                                <RotateTransform Angle="-70"/>
                            </Image.RenderTransform>
                        </Image>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="PART_Path" Property="Fill" Value="#009AD8"/>
                            <Setter Property="Cursor" Value="Hand"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
</Style>
</ResourceDictionary>

3.MainWindow.xaml

<Window x:Class="WpfCircularMenu.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:WpfCircularMenu"
        mc:Ignorable="d"
        Title="MainWindow" Height="850" Width="1200"
        Background="Black"
        SnapsToDevicePixels="True" 
        TextOptions.TextFormattingMode="Display" 
        UseLayoutRounding="True">
    <Window.Resources>
        <Storyboard x:Key="CheckedStoryboard">
            <DoubleAnimation Storyboard.TargetName="PART_EllipseGeometry"
                             Storyboard.TargetProperty="RadiusX"
                             Duration="00:00:0.4" To="200"/>
            <DoubleAnimation Storyboard.TargetName="PART_EllipseGeometry"
                             Storyboard.TargetProperty="RadiusY"
                             Duration="00:00:0.4" To="200"/>
        </Storyboard>
        <Storyboard x:Key="UncheckedStoryboard">
            <DoubleAnimation Storyboard.TargetName="PART_EllipseGeometry"
                             Storyboard.TargetProperty="RadiusX"
                             Duration="00:00:0.3" To="0"/>
            <DoubleAnimation Storyboard.TargetName="PART_EllipseGeometry"
                             Storyboard.TargetProperty="RadiusY"
                             Duration="00:00:0.3" To="0"/>
        </Storyboard>
    </Window.Resources>
    <Viewbox>
        <Grid Height="768" Width="1024">
            <Canvas>
                <ItemsControl ItemsSource="{Binding MenuArray,RelativeSource={RelativeSource AncestorType=local:MainWindow}}"
                              Canvas.Left="150" Canvas.Top="150">
                    <ItemsControl.Clip>
                        <EllipseGeometry x:Name="PART_EllipseGeometry" RadiusX="0" RadiusY="0" Center="200,200"></EllipseGeometry>
                    </ItemsControl.Clip>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <local:CircularMenuItemCustomControl Angle="{Binding Angle}" MenuTxt="{Binding Title}" 
                                                              BackgroundColor="{Binding FillColor}" IconImage="{Binding IconImage}"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <Grid/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>
               
                <ToggleButton Canvas.Left="300" Canvas.Top="300" Cursor="Hand">
                    <ToggleButton.Template>
                        <ControlTemplate TargetType="ToggleButton">
                            <Grid>
                                <Ellipse x:Name="PART_Ellipse" Width="100" Height="100" Fill="#009AD8" ToolTip="關閉"/>
                                <Path x:Name="PART_Path" Data="M734.618 760.269c-24.013 24.013-62.925 24.013-86.886 0l-135.731-155.136-135.731 155.085c-24.013 24.013-62.925 24.013-86.886 0-24.013-24.013-24.013-62.925 0-86.886l141.21-161.28-141.261-161.382c-24.013-24.013-24.013-62.874 0-86.886s62.874-24.013 86.886 0l135.782 155.187 135.731-155.187c24.013-24.013 62.874-24.013 86.886 0s24.013 62.925 0 86.886l-141.21 161.382 141.21 161.28c24.013 24.013 24.013 62.925 0 86.938z"
                                      Fill="White" Stretch="Fill" Width="20" Height="20" RenderTransformOrigin="0.5,0.5" IsHitTestVisible="False">
                                </Path>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsChecked" Value="false">
                                    <Setter TargetName="PART_Path" Property="RenderTransform">
                                        <Setter.Value>
                                            <RotateTransform Angle="45"/>
                                        </Setter.Value>
                                    </Setter>
                                    <Setter Property="ToolTip" TargetName="PART_Ellipse" Value="展開"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </ToggleButton.Template>
                    <ToggleButton.Triggers>
                        <EventTrigger RoutedEvent="ToggleButton.Checked">
                            <BeginStoryboard Storyboard="{StaticResource CheckedStoryboard}"/>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="ToggleButton.Unchecked">
                            <BeginStoryboard Storyboard="{StaticResource UncheckedStoryboard}"/>
                        </EventTrigger>
                    </ToggleButton.Triggers>
                </ToggleButton>
                <TextBlock Text="微信公眾號:WPF開發者" FontSize="40"
                           Foreground="#A9CC32" FontWeight="Bold"
                           Canvas.Top="50"/>
                <Image Source="Images/gzh.png" Canvas.Left="140" Canvas.Bottom="40"/>
            </Canvas>
        </Grid>
    </Viewbox>
</Window>

4.MainWindow.xaml.cs

<Window x:Class="WpfCircularMenu.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:WpfCircularMenu"
        mc:Ignorable="d"
        Title="MainWindow" Height="850" Width="1200"
        Background="Black"
        SnapsToDevicePixels="True" 
        TextOptions.TextFormattingMode="Display" 
        UseLayoutRounding="True">
    <Window.Resources>
        <Storyboard x:Key="CheckedStoryboard">
            <DoubleAnimation Storyboard.TargetName="PART_EllipseGeometry"
                             Storyboard.TargetProperty="RadiusX"
                             Duration="00:00:0.4" To="200"/>
            <DoubleAnimation Storyboard.TargetName="PART_EllipseGeometry"
                             Storyboard.TargetProperty="RadiusY"
                             Duration="00:00:0.4" To="200"/>
        </Storyboard>
        <Storyboard x:Key="UncheckedStoryboard">
            <DoubleAnimation Storyboard.TargetName="PART_EllipseGeometry"
                             Storyboard.TargetProperty="RadiusX"
                             Duration="00:00:0.3" To="0"/>
            <DoubleAnimation Storyboard.TargetName="PART_EllipseGeometry"
                             Storyboard.TargetProperty="RadiusY"
                             Duration="00:00:0.3" To="0"/>
        </Storyboard>
    </Window.Resources>
    <Viewbox>
        <Grid Height="768" Width="1024">
            <Canvas>
                <ItemsControl ItemsSource="{Binding MenuArray,RelativeSource={RelativeSource AncestorType=local:MainWindow}}"
                              Canvas.Left="150" Canvas.Top="150">
                    <ItemsControl.Clip>
                        <EllipseGeometry x:Name="PART_EllipseGeometry" RadiusX="0" RadiusY="0" Center="200,200"></EllipseGeometry>
                    </ItemsControl.Clip>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <local:CircularMenuItemCustomControl Angle="{Binding Angle}" MenuTxt="{Binding Title}" 
                                                              BackgroundColor="{Binding FillColor}" IconImage="{Binding IconImage}"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <Grid/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>
               
                <ToggleButton Canvas.Left="300" Canvas.Top="300" Cursor="Hand">
                    <ToggleButton.Template>
                        <ControlTemplate TargetType="ToggleButton">
                            <Grid>
                                <Ellipse x:Name="PART_Ellipse" Width="100" Height="100" Fill="#009AD8" ToolTip="關閉"/>
                                <Path x:Name="PART_Path" Data="M734.618 760.269c-24.013 24.013-62.925 24.013-86.886 0l-135.731-155.136-135.731 155.085c-24.013 24.013-62.925 24.013-86.886 0-24.013-24.013-24.013-62.925 0-86.886l141.21-161.28-141.261-161.382c-24.013-24.013-24.013-62.874 0-86.886s62.874-24.013 86.886 0l135.782 155.187 135.731-155.187c24.013-24.013 62.874-24.013 86.886 0s24.013 62.925 0 86.886l-141.21 161.382 141.21 161.28c24.013 24.013 24.013 62.925 0 86.938z"
                                      Fill="White" Stretch="Fill" Width="20" Height="20" RenderTransformOrigin="0.5,0.5" IsHitTestVisible="False">
                                </Path>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsChecked" Value="false">
                                    <Setter TargetName="PART_Path" Property="RenderTransform">
                                        <Setter.Value>
                                            <RotateTransform Angle="45"/>
                                        </Setter.Value>
                                    </Setter>
                                    <Setter Property="ToolTip" TargetName="PART_Ellipse" Value="展開"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </ToggleButton.Template>
                    <ToggleButton.Triggers>
                        <EventTrigger RoutedEvent="ToggleButton.Checked">
                            <BeginStoryboard Storyboard="{StaticResource CheckedStoryboard}"/>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="ToggleButton.Unchecked">
                            <BeginStoryboard Storyboard="{StaticResource UncheckedStoryboard}"/>
                        </EventTrigger>
                    </ToggleButton.Triggers>
                </ToggleButton>
                <TextBlock Text="微信公眾號:WPF開發者" FontSize="40"
                           Foreground="#A9CC32" FontWeight="Bold"
                           Canvas.Top="50"/>
                <Image Source="Images/gzh.png" Canvas.Left="140" Canvas.Bottom="40"/>
            </Canvas>
        </Grid>
    </Viewbox>
</Window>

關于“WPF如何實現環形菜單”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

wpf
AI

富民县| 卢湾区| 和政县| 丽水市| 霍林郭勒市| 乌兰察布市| 扬中市| 襄垣县| 丹棱县| 襄汾县| 平江县| 盐池县| 新乐市| 龙游县| 惠州市| 凯里市| 通州区| 鄂尔多斯市| 湘乡市| 三穗县| 毕节市| 黄大仙区| 报价| 岐山县| 邻水| 内乡县| 东港市| 平湖市| 额尔古纳市| 甘南县| 南郑县| 辉县市| 和林格尔县| 阿荣旗| 凌云县| 阿拉善右旗| 海原县| 虎林市| 西峡县| 从化市| 榕江县|