大佬教程收集整理的这篇文章主要介绍了silverlight MenuItem 二级菜单,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在SL里用ContextMenu和MenuItem可以组成菜单项。但是在给MenuItem添加二级菜单的时候就会报错---"方法或操作未实现"。
在网上找到一篇文章,解决了这个问题 原文地址:http://www.cnblogs.com/tanliang/archive/2011/11/25/2263342.html
细看了一下解决办法,是继承了@H_27_19@menuItem控件并在模板里添加了ItemsPresenter,我去~ @H_27_19@menuItem不是有Items属性吗,为毛原先没有ItemsPresenter啊!!!这不神经吗..........
代码备份如下:
新建模版化控件SuperMenuItem:
SuperMenuItem using 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 二级菜单所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。