如何文本菜单添加到按钮 [英] How to Add ContextMenu to Button

查看:136
本文介绍了如何文本菜单添加到按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个按钮,这是主题看起来像瓷砖的StackPanel。每一个将要被固定到开始屏幕的能力。我想一个文本菜单添加到每个启用此功能。我怎么可以这样做?另外,我怎么确定窃听项目?

MainPage.xaml中

 < StackPanel中的Horizo​​ntalAlignment =左方向=横向>                        <按钮X:NAME =Tile1HEIGHT =173WIDTH =173保证金=12,0,0,0点击=1_Click风格={StaticResource的ButtonStyle1}工具箱:TiltEffect.IsTiltEnabled = 真>
                            < Button.Content>
                                <图像源=/资产/瓷砖/ 1.png/>
                            < /Button.Content>
                        < /按钮>
                        <按钮X:NAME =Tile2HEIGHT =173WIDTH =173保证金=12,0,0,0点击=2_Click风格={StaticResource的ButtonStyle1}工具箱:TiltEffect.IsTiltEnabled = 真>
                            < Button.Content>
                                <图像源=/资产/瓷砖/ 2.png/>
                            < /Button.Content>
                        < /按钮>
                        <按钮X:NAME =Tile3HEIGHT =173WIDTH =173保证金=12,0,0,0点击=3_Click风格={StaticResource的ButtonStyle1}工具箱:TiltEffect.IsTiltEnabled = 真>
                            < Button.Content>
                                <图像源=/资产/瓷砖/ 3.png/>
                            < /Button.Content>
                        < /按钮>
< / StackPanel的>


解决方案

既然你创建的那些按钮的手动-I的意思是不是<一个href=\"http://stackoverflow.com/questions/14166876/how-to-make-context-menu-work-for-windows-phone\">generated使用的DataTemplate 或所谓,你需要添加文本菜单为他们每个人也手动。

  ...
&LT;按钮X:NAME =Tile1HEIGHT =173WIDTH =173保证金=12,0,0,0工具箱:TiltEffect.IsTiltEnabled =真&GT;
    &LT; Button.Content&GT;
        &lt;图像源=/资产/瓷砖/ IconicTileSmall.png/&GT;
    &LT; /Button.Content>
    &LT;工具箱:ContextMenuService.ContextMenu&GT;
        &LT;工具:文本菜单&GT;
            &LT;工具箱:菜单项标题=附到'开始点击=MenuItem_Click/&GT;
        &LT; /工具:文本菜单&GT;
    &LT; /工具包:ContextMenuService.ContextMenu&GT;
&LT; /按钮&GT;
&LT;按钮X:NAME =Tile2HEIGHT =173WIDTH =173保证金=12,0,0,0工具箱:TiltEffect.IsTiltEnabled =真&GT;
    &LT; Button.Content&GT;
        &lt;图像源=/资产/瓷砖/ IconicTileSmall.png/&GT;
    &LT; /Button.Content>
    &LT;工具箱:ContextMenuService.ContextMenu&GT;
        &LT;工具:文本菜单&GT;
            &LT;工具箱:菜单项标题=附到'开始点击=MenuItem_Click/&GT;
        &LT; /工具:文本菜单&GT;
    &LT; /工具包:ContextMenuService.ContextMenu&GT;
&LT; /按钮&GT;
...

但至少,有重用方法处理菜单项的单击事件的方式:

 私人无效MenuItem_Click(对象发件人,RoutedEventArgs E)
{
    VAR菜单项=(菜单项)发件人;
    VAR ctxMenu =(文本菜单)menuItem.Parent;
    VAR tileButton =(按钮)ctxMenu.Owner;    //下一页:对应tileButton脚开始
}

I have a StackPanel of a few buttons, which are themed to look like tiles. Each will have the ability to be pinned to the Start screen. I would like to add a ContextMenu to each to enable this functionality. How might I do this? Also, how do I determine the tapped item?

MainPage.xaml

<StackPanel HorizontalAlignment="Left" Orientation="Horizontal">

                        <Button x:Name="Tile1" Height="173" Width="173" Margin="12,0,0,0" Click="1_Click" Style="{StaticResource ButtonStyle1}" toolkit:TiltEffect.IsTiltEnabled="True">
                            <Button.Content>
                                <Image Source="/Assets/Tiles/1.png"/>
                            </Button.Content>
                        </Button>
                        <Button x:Name="Tile2" Height="173" Width="173" Margin="12,0,0,0" Click="2_Click" Style="{StaticResource ButtonStyle1}" toolkit:TiltEffect.IsTiltEnabled="True">
                            <Button.Content>
                                <Image Source="/Assets/Tiles/2.png"/>
                            </Button.Content>
                        </Button>
                        <Button x:Name="Tile3" Height="173" Width="173" Margin="12,0,0,0" Click="3_Click" Style="{StaticResource ButtonStyle1}" toolkit:TiltEffect.IsTiltEnabled="True">
                            <Button.Content>
                                <Image Source="/Assets/Tiles/3.png"/>
                            </Button.Content>
                        </Button>
</StackPanel>

解决方案

Since you created those Button's manually -I mean not generated using DataTemplate or so-, you need to add ContextMenu for each of them manually too.

...
<Button x:Name="Tile1" Height="173" Width="173" Margin="12,0,0,0" toolkit:TiltEffect.IsTiltEnabled="True">
    <Button.Content>
        <Image Source="/Assets/Tiles/IconicTileSmall.png"/>
    </Button.Content>
    <toolkit:ContextMenuService.ContextMenu>
        <toolkit:ContextMenu>
            <toolkit:MenuItem Header="Pin To Start" Click="MenuItem_Click"/>
        </toolkit:ContextMenu>
    </toolkit:ContextMenuService.ContextMenu>
</Button>
<Button x:Name="Tile2" Height="173" Width="173" Margin="12,0,0,0" toolkit:TiltEffect.IsTiltEnabled="True">
    <Button.Content>
        <Image Source="/Assets/Tiles/IconicTileSmall.png"/>
    </Button.Content>
    <toolkit:ContextMenuService.ContextMenu>
        <toolkit:ContextMenu>
            <toolkit:MenuItem Header="Pin To Start" Click="MenuItem_Click"/>
        </toolkit:ContextMenu>
    </toolkit:ContextMenuService.ContextMenu>
</Button>
...

But at least, there is a way to reuse method that handle MenuItem's click event:

private void MenuItem_Click(object sender, RoutedEventArgs e)
{
    var menuItem = (MenuItem) sender;
    var ctxMenu = (ContextMenu) menuItem.Parent;
    var tileButton = (Button) ctxMenu.Owner;

    //Next: pin corresponding tileButton to start
}

这篇关于如何文本菜单添加到按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆