Fork me on GitHub

ContextMenus

 这个控件比较特殊,它是控件右键的单菜,类似于窗体的右键菜单。先看下面的例子。

XMAL代码:

<Page

       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/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"

       x:Class="Controls.ContextMenu"

       x:Name="Page"

       WindowTitle="Page"

       FlowDirection="LeftToRight"

       Width="640" Height="480"

       WindowWidth="640" WindowHeight="480">

      

<Page.Resources>

<Style x:Key="Triggers" TargetType="{x:Type MenuItem}">

    <Style.Triggers>

     <Trigger Property="MenuItem.IsMouseOver" Value="true">

         <Setter Property = "FontSize" Value="16"/>

         <Setter Property = "FontStyle" Value="Italic"/>

         <Setter Property = "Foreground" Value="Red"/>

     </Trigger>

    </Style.Triggers>

</Style> 

</Page.Resources>

 

<StackPanel>

<StackPanel.Resources>

 <ContextMenu x:Key="MyContextMenu" x:Shared="true">

    <MenuItem Header="This MenuItem is checkable" IsCheckable="true" />

    <Separator/>

    <MenuItem Header="This is a regular MenuItem" />

 </ContextMenu>

</StackPanel.Resources>

 

<Button Name="Button1" Background="LightBlue" Content="This Button has a ContextMenu"   ContextMenu="{DynamicResource MyContextMenu}" Width="258" />

 

<Button Name="Button2" Height="30" Content="Disabled Button" IsEnabled="False" ContextMenuService.ShowOnDisabled="True"  Width="368">

 <Button.ContextMenu>

 <ContextMenu>      

   <MenuItem Style="{StaticResource Triggers}" Header="Item 1"/>

   <MenuItem Style="{StaticResource Triggers}" Header="Item 2"/>    

   <MenuItem Style="{StaticResource Triggers}" Header="Item 3"/> 

 </ContextMenu>

 </Button.ContextMenu>

</Button>

 

<TabItem Name="backgroundcolor" Header="Choose a Background Color" Width="373" Height="102.277">

 <TabItem.ContextMenu>

   <ContextMenu MenuItem.Click="MyMenuHandler">

     <MenuItem Header="Red" Name="red"/>

     <MenuItem Header="Blue" Name="blue"/>

     <MenuItem Header="Yellow" Name="yellow"/>

   </ContextMenu>

 </TabItem.ContextMenu>

 <TabItem.Content>Some content about background colors.</TabItem.Content>

</TabItem>

 

</StackPanel>

</Page>

Button1是利用了一个资源,来嵌入ContextMenu。它的MenuItemIsCheckabletrue,即能处天选择状态。    <Separator/>是菜单项中间的虚线。


 

Button2有两个属性需要注意:IsEnabled="False" ContextMenuService.ShowOnDisabled="True",对于Enabled大家可能熟悉,即这个控件处理“死状态”,不可操作,但ContextMenuService.ShowOnDisabledTrue又为它的右键赋予了活的性属。


Backgroundcolor控件控过MenuItem.Click="MyMenuHandler"属性来更控TabItem控件的背景颜色。具体更换背景色的代码如下CS代码。

 


CS代码:

 

void MyMenuHandler(object sender, RoutedEventArgs e)

        {

            if (e.Source == red)

            {

                backgroundcolor.Background = Brushes.Red;

                backgroundcolor.Header = "Background red";

            }

            else if (e.Source == blue)

            {

                backgroundcolor.Background = Brushes.LightBlue;

                backgroundcolor.Header = "Background blue";

            }

            else if (e.Source == yellow)

            {

                backgroundcolor.Background = Brushes.Yellow;

                backgroundcolor.Header = "Background yellow";

            }

        }
本文参考MSDN组织。

posted @ 2007-03-19 23:57  桂素伟  阅读(432)  评论(0编辑  收藏  举报