
在SL里用ContextMenu和MenuItem可以组成菜单项。但是在给MenuItem添加二级菜单的时候就会报错---"方法或 *** 作未实现"。
在网上找到一篇文章,解决了这个问题 原文地址:http://www.cnblogs.com/tanliang/archive/2011/11/25/2263342.html
细看了一下解决办法,是继承了MenuItem控件并在模板里添加了ItemsPresenter,我去~ MenuItem不是有Items属性吗,为毛原先没有ItemsPresenter啊!!!这不神经吗..........
代码备份如下:
新建模版化控件SuperMenuItem:
SuperMenuItemusing System;using System.Collections.Specialized;using System.windows;using System.windows.Controls;using System.windows.Controls.Primitives;using System.windows.input;namespace Demo{ public class SuperMenuItem : MenuItem { #region FIElds private Popup popup; public bool CanLeave { get; set; } #endregion #region PropertIEs public Visibility HasSubItems { get { return (Visibility)GetValue(HasSubItemsProperty); } set { SetValue(HasSubItemsProperty,value); } } public static Readonly DependencyProperty HasSubItemsProperty = DependencyProperty.Register("HasSubItems",typeof(Visibility),typeof(SuperMenuItem),new PropertyMetadata(Visibility.Collapsed)); public bool IsSubmenuOpen { get { return (bool)GetValue(IsSubmenuOpenProperty); } set { SetValue(IsSubmenuOpenProperty,value); } } public static Readonly DependencyProperty IsSubmenuOpenProperty = DependencyProperty.Register("IsSubmenuOpen",typeof(bool),new PropertyMetadata(false)); #endregion #region Constructor public SuperMenuItem() { this.DefaultStyleKey = typeof(SuperMenuItem); this.MouseEnter += new MouseEventHandler(parent_MouseEnter); this.MouseLeave += new MouseEventHandler(SuperMenuItem_MouseLeave); this.Click += new RoutedEventHandler(SuperMenuItem_Click); this.CanLeave = true; } private voID SuperMenuItem_Click(object sender,RoutedEventArgs e) { if (this.Parent != null && this.Parent is SuperMenuItem) { (this.Parent as SuperMenuItem).OnClick(); } } public overrIDe voID OnApplyTemplate() { base.OnApplyTemplate(); popup = (Popup)this.GetTemplateChild("PART_Popup"); popup.Opened += new EventHandler(popup_Opened); popup.Closed += new EventHandler(popup_Closed); } private voID popup_Opened(object sender,EventArgs e) { this.CanLeave = false; } private voID popup_Closed(object sender,EventArgs e) { if (this.HasSubItems == Visibility.Visible) { this.IsSubmenuOpen = false; } } protected overrIDe voID OnItemsChanged(NotifyCollectionChangedEventArgs e) { if (e.NewItems != null) { if (e.NewItems.Count > 0) { this.HasSubItems = Visibility.Visible; } } } private voID parent_MouseEnter(object sender,MouseEventArgs e) { this.CanLeave = true; if (this.HasSubItems == Visibility.Visible) { this.IsSubmenuOpen = true; } if (this.Parent != null && this.Parent is ContextMenu) { foreach (var item in (this.Parent as ContextMenu).Items) { if (item != this) { (item as SuperMenuItem).IsSubmenuOpen = false; } } } } private voID SuperMenuItem_MouseLeave(object sender,MouseEventArgs e) { if (this.HasSubItems == Visibility.Visible) { if (CanLeave) { this.IsSubmenuOpen = false; } } } #endregion }} 对应样式:
<Style targettype="local:SuperMenuItem"> <Setter Property="Background" Value="transparent"/> <Setter Property="borderBrush" Value="transparent"/> <Setter Property="padding" Value="4,3,2,3"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate targettype="local:SuperMenuItem"> <GrID> <visualstatemanager.VisualStateGroups> <VisualStateGroup x:name="CommonStates"> <VisualState x:name="normal"/> <VisualState x:name="Disabled"> <Storyboard> <DoubleAnimation Duration="0" To="0.5" Storyboard.TargetProperty="Opacity" Storyboard.Targetname="Presenter"/> </Storyboard> </VisualState> </VisualStateGroup> <VisualStateGroup x:name="Focusstates"> <VisualState x:name="Unfocused"/> <VisualState x:name="Focused"> <Storyboard> <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.Targetname="Bg"/> <colorAnimation Duration="0" To="#40FFFFFF" Storyboard.TargetProperty="(Shape.stroke).(SolIDcolorBrush.color)" Storyboard.Targetname="Innerborder"/> </Storyboard> </VisualState> </VisualStateGroup> </visualstatemanager.VisualStateGroups> <Rectangle Fill="{TemplateBinding Background}" RadiusY="2" RadiusX="2" stroke="{TemplateBinding borderBrush}" strokeThickness="1"/> <Rectangle x:name="Bg" Opacity="0" RadiusY="2" RadiusX="2" stroke="#8071CBF1" strokeThickness="1"> <Rectangle.Fill> <linearGradIEntBrush EndPoint="0,1" StartPoint="0,0"> <GradIEntStop color="#34C5EBFF" Offset="0"/> <GradIEntStop color="#3481D8FF" Offset="1"/> </linearGradIEntBrush> </Rectangle.Fill> </Rectangle> <Rectangle x:name="Innerborder" margin="1" RadiusY="2" RadiusX="2" stroke="transparent"/> <GrID> <GrID.ColumnDeFinitions> <ColumnDeFinition MinWIDth="24" WIDth="auto"/> <ColumnDeFinition WIDth="4"/> <ColumnDeFinition WIDth="*"/> <ColumnDeFinition WIDth="17"/> </GrID.ColumnDeFinitions> <ContentPresenter Content="{TemplateBinding Icon}" margin="1" VerticalAlignment="Center"/> <ContentPresenter x:name="Presenter" ContentTemplate="{TemplateBinding headerTemplate}" Content="{TemplateBinding header}" GrID.Column="2" margin="{TemplateBinding padding}"/> <Path GrID.Column="3" Data="M 0,0 L 4,3.5 L 0,7 Z" Fill="Black" margin="4,0" VerticalAlignment="Center" Visibility="{TemplateBinding HasSubItems}"/> <!--<Path x:name="Glyph" Data="M 0,5.1 L 1.7,5.2 L 3.4,7.1 L 8,0.4 L 9.2,0 L 3.3,10.8 Z" Fill="#0C12A1" FlowDirection="leftToRight" Height="11" WIDth="9"/>--> </GrID> <Popup x:name="PART_Popup" HorizontalOffset="{TemplateBinding ActualWIDth}" IsOpen="{TemplateBinding IsSubmenuOpen}" margin="-4,0"> <ContentControl x:name="SubMenuborder"> <ContentControl.Template> <ControlTemplate> <GrID Background="#FFF5F5F5"> <Rectangle Fill="#F1F1F1" HorizontalAlignment="left" RadiusY="2" RadiusX="2" WIDth="28"/> <Rectangle Fill="#E2E3E3" HorizontalAlignment="left" WIDth="1" margin="30,0"/> <Rectangle Fill="White" HorizontalAlignment="left" WIDth="1" margin="31,0"/> <ContentPresenter GrID.ColumnSpan="2" margin="1,0"/> </GrID> </ControlTemplate> </ContentControl.Template> <ScrollVIEwer x:name="SubMenuScrollVIEwer" VerticalScrollbarVisibility="auto" padding="0"> <GrID> <Canvas HorizontalAlignment="left" Height="0" VerticalAlignment="top" WIDth="0"> <Rectangle Fill="#FFF5F5F5" Height="{Binding ActualHeight,Elementname=SubMenuborder}" WIDth="{Binding ActualWIDth,Elementname=SubMenuborder}"/> </Canvas> <ItemsPresenter x:name="ItemsPresenter" margin="2"/> </GrID> </ScrollVIEwer> </ContentControl> </Popup> </GrID> </ControlTemplate> </Setter.Value> </Setter> </Style> 用法不写了,看原文吧。总之,有子菜单的菜单项就用这个SuperMenuItem,没有的话就随便了。 总结 以上是内存溢出为你收集整理的silverlight MenuItem 二级菜单全部内容,希望文章能够帮你解决silverlight MenuItem 二级菜单所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)