生活随笔
收集整理的這篇文章主要介紹了
WPF 用Popup做下拉菜单
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天無聊時看到必應搜索首頁的菜單挺好,于是想著模仿一下。。寫著寫著發現和我之前做的一個MenuItem很像,干脆直接拿來用了。。。
看看效果:
上圖是bing.com首頁右上角的下拉菜單,今天就來做了一個這樣的。。
我承認我偷懶了,哈是在以前的menuitem外面套了一個popup,
下面就看看主要代碼,最后附上下載鏈接。
新建一個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"><Grid><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));public DropDownMenu(){ItemViewModel = new SideMenuItemViewModel(){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
如果喜歡,點個贊唄~
總結
以上是生活随笔為你收集整理的WPF 用Popup做下拉菜单的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。