相关文章推荐
hihk2  ·  解决PresentationFontCach ...·  3 年前    · 
刚分手的茶壶  ·  progressBar.setOnSeekB ...·  1 月前    · 
爱看球的牙膏  ·  IDEA ...·  2 月前    · 
拉风的眼镜  ·  org.apache.catalina.co ...·  1 年前    · 
完美的板栗  ·  delphi - Projects ...·  1 年前    · 

新建一个DropDownMenu用户控件:

<UserControl x:Class="wpfcore.DropDownMenu"
             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:wpfcore"
             mc:Ignorable="d" 
             x:Name="dropdownmenu"
             Background="Transparent"
             Width="40" Height="40"
             d:DesignHeight="32" d:DesignWidth="32">
        <Border x:Name="border"
                Background="Transparent"
                Margin="8"
                MouseUp="OnBorderMouseDown">
            <Viewbox>
                <Path Fill="Red" SnapsToDevicePixels="True" Stretch="Uniform"
                  Data="M170.666667 213.333333h682.666666v85.333334H170.666667V213.333333z m0 512h682.666666v85.333334H170.666667v-85.333334z m0-256h682.666666v85.333334H170.666667v-85.333334z"/>
            </Viewbox>
        </Border>
        <Popup Placement="Bottom" 
               PlacementTarget="{Binding ElementName=border}"
               AllowsTransparency="True"
               IsOpen="{Binding IsOpen,ElementName=dropdownmenu}"
               StaysOpen="False"
               PopupAnimation="Slide"
               VerticalOffset="6"
            <Border Margin="6"    Background="LightBlue">
                <Border.Effect>
                    <DropShadowEffect BlurRadius="6" ShadowDepth="0" Color="Red"/>
                </Border.Effect>
                <StackPanel>
                    <local:SideMenuItem DataContext="{Binding ItemViewModel,ElementName=dropdownmenu}"
                            IsExpanded="True"
                            MenuItemBackground="#24ACF2"
                            MenuItemSelectedBackground="YellowGreen"
                            ToggleBackground="#007ACC"
                            MenuItemSelectedChanged="S"/>
                    <local:SideMenuItem DataContext="{Binding ItemViewModel,ElementName=dropdownmenu}"
                            MenuItemBackground="#24ACF2"
                            MenuItemSelectedBackground="YellowGreen"
                            ToggleBackground="#007ACC"
                            MenuItemSelectedChanged="S"/>            
                </StackPanel>
            </Border>
        </Popup>
    </Grid>
</UserControl>
 

此控件后台代码:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace wpfcore
    public partial class DropDownMenu : UserControl
        public bool IsOpen
            get { return (bool)GetValue(IsOpenProperty); }
            set { SetValue(IsOpenProperty, value); }
        public static readonly DependencyProperty IsOpenProperty =
            DependencyProperty.Register("IsOpen", typeof(bool), typeof(DropDownMenu), new PropertyMetadata(false));
                HeaderText = "快速开始",
                IconGeometry = FindResource("IconInfo") as Geometry,
                Items = new List<object>()
                    "5.0新变化",
                    "第一个项目",
                    "第一个模块",
                    "自定义用户",
                    "捐赠",
                    "FAQ"
            InitializeComponent();
        private void OnBorderMouseDown(object sender, MouseButtonEventArgs e)
            IsOpen = !IsOpen;
        public SideMenuItemViewModel ItemViewModel { get; set; }
        private void S(object sender, RoutedEventArgs e)
 

其它代码太多了,直接上链接算了,

链接:https://pan.baidu.com/s/1Ei1BNVCrvZFfv2mQlBX-MA

提取码:bfpv

如果喜欢,点个赞呗~

今天无聊时看到必应搜索首页的菜单挺好,于是想着模仿一下。。写着写着发现和我之前做的一个MenuItem很像,干脆直接拿来用了。。。看看效果:上图是bing.com首页右上角的下拉菜单,今天...
最近在一个WPF项目中用到一个下拉列表,随着用户输入字符而进行显示,使用了绑定等知识,虽然实现比较简单,可是在性能上也是想了很多办法终于才勉强可以用,与大家分享下。 用于页面绑定的模型类: public class MainWindowModel : INotifyPropertyChanged public event PropertyChangedEvent
WPF中ComboBox下拉菜单控件的介绍 ​ 前几天在用WPF制作贪吃蛇游戏的时候,遇到了一个非常棘手的问题——下拉菜单选择时的事件问题,在网上查找了很多资料都没解决,最后还是我们帅气的曹老师帮我们讲解了这个问题,才得以解决。那么今天我将这些总结出来,来帮助更多在学习WPF过程中遇到这种问题的朋友。 1、Items属性 2、Items的属性和方法
<Button x:Name="menu" HorizontalAlignment="Right" Height="24" VerticalAlignment="Top" Width="40" Margin="0,1,79.8,0" Click="menu_Click" Content="Hello"/> <Popup x:Name="menuPop1" StaysOpen="False" Margin="0" PlacementTarget="{Binding Elem...
开发工具与关键技术:Microsoft Visual Studio 2017、WPF 这里这个下拉式菜单栏用的是一个RadioButton(单选按钮)和Popup(包含内容的弹出项)组合形成的下拉式菜单栏。 菜单栏显示:点击RadioButton即可显示。 菜单栏隐藏:点击菜单栏外 屏幕任意一处 即可隐藏或选中该菜单栏内的某一项时隐藏菜单栏,但是选中该菜单栏内的某一项时隐藏菜单栏这个代码我这里因为菜单栏的每一项的点击事件里面含有其他代码所以不方便就没有给出来,需要的话自行加上,如果你是按照我下面写的方法来
我有两个combox,都需要以列表作为数据源,其中一个还涉及显示时的转换(后台是“+/-/ ”,前台则要显示“正向/反向/无方向”)。本篇涉及数据源的绑定和后台获取结果。 <Window.Resources> <local:DirectionToZhConverter x:Key="dir2zh"/> </Window.Resources> <!--这个需要转换--> <ComboBox Gri
WPF中的Popup控件是一个常用的容器控件,在实现各种功能时经常用到。而对于Popup控件的阴影设置,可以通过以下几种方式实现: 1. 使用DropShadowEffect WPF中可以通过DropShadowEffect效果给控件添加阴影,可以使用以下代码将阴影效果添加到Popup控件中: <Popup Name="MyPopup" Width="300" Height="250"> <Border Background="White" BorderThickness="2" BorderBrush="Gray"> <Border.Effect> <DropShadowEffect Color="Gray" BlurRadius="15" Opacity=".75" ShadowDepth="5" /> </Border.Effect> <!-- 添加Popup控件内容 --> </Border> </Popup> 2. 添加阴影Border 除了使用DropShadowEffect效果,还可以通过在Popup控件外部添加一个带阴影的Border元素来实现阴影效果。以下是示例代码: <Border Background="Transparent" BorderThickness="0"> <Border Background="White" BorderThickness="2" BorderBrush="Gray"> <Popup Name="MyPopup" Width="300" Height="250"> <!-- 添加Popup控件内容 --> </Popup> </Border> <Border.Effect> <DropShadowEffect Color="Gray" BlurRadius="15" Opacity=".75" ShadowDepth="5" /> </Border.Effect> </Border> 3. 使用VisualBrush 还可以通过使用VisualBrush将Popup控件的内容绘制到Canvas上,并在Canvas上添加阴影效果来实现。以下是示例代码: <Popup Name="MyPopup" Width="300" Height="250"> <!-- 定义VisualBrush --> <Popup.Resources> <VisualBrush x:Key="PopupContent"> <VisualBrush.Visual> <ContentControl Content="{Binding ElementName=MyPopup, Path=Child}"/> </VisualBrush.Visual> </VisualBrush> </Popup.Resources> <Canvas> <!-- 绘制Popup内容 --> <Rectangle Fill="{StaticResource PopupContent}" Width="{Binding ActualWidth, ElementName=MyPopup}" Height="{Binding ActualHeight, ElementName=MyPopup}" /> <!-- 添加阴影效果 --> <Rectangle Fill="Gray" Opacity="0.5" Width="{Binding ActualWidth, ElementName=MyPopup}" Height="{Binding ActualHeight, ElementName=MyPopup}"> <Rectangle.Effect> <DropShadowEffect BlurRadius="15" Opacity=".75" ShadowDepth="5" Direction="-90" /> </Rectangle.Effect> </Rectangle> </Canvas> </Popup> 通过以上几种方式实现Popup控件的阴影效果,可以根据实际需求选择最合适的方法,从而大大提高了应用程序的用户体验。