简体   繁体   中英

Can WP7 Silver Light Gesture Listener be added to a StackPanel?

I'm working on a small Windows Phone 7 Silver Light app. I simply want a context menu to pop up over an item in a Listbox when the user taps and holds on the respective entry. I've read through several posts and the most common answer given is to use the Silverlight toolkit. I was excited since I already have it set up and use it for a few other things in the app. So I found a tutorial that explains how to set it up on a button. It failed when I tried because I was setting the content for the button twice:

    <Button Width="Auto" MinWidth="460" Height="Auto" HorizontalAlignment="Stretch">
        <Button.Content>
            <toolkit:GestureListener Hold="GestureListener_Hold" />
            <StackPanel ..... >
                .......
            </<StackPanel>
        </Button.Content>
    </Button>

Which tells me "The property 'Content' is et more than once. So after searching again I found a nice article, http://forums.create.msdn.com/forums/t/85263.aspx , which shows that I can move the statement to look more like:

    <StackPanel Orientation.....>
        <toolkit:GestureListener Hold="GestureListener_Hold" />  
        <TextBlock
            Text="{Binding ItemName}" FontSize="{StaticResource PhoneFontSizeLarge}"
            HorizontalAlignment="Stretch" VerticalAlignment="Center" Height="40"/>
    </StackPanel>

Which tells me "A value of type 'GestureListener' cannot be added to a collection or dictionary of type 'UIElementCollection'.

I'd appreciate any help on either method or a brand new one. Thanks in advance!

You need to set the GestureListener as the value of the GestureService.GestureListener attached property:

<Button Width="Auto" MinWidth="460" Height="Auto" HorizontalAlignment="Stretch"> 
    <Button.Content> 
        <StackPanel ..... > 
            <toolkit:GestureService.GestureListener>
                <toolkit:GestureListener Hold="GestureListener_Hold" />
            </toolkit:GestureService.GestureListener>
            ....... 
        </<StackPanel> 
    </Button.Content> 
</Button> 

For what it's worth, the toolkit already includes a ContextMenu helper. See here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM