Search code examples
cwindows-phone-8longlistselector

Click on buttons in DataTemplate - WP8


I'm using this datatemplate for my app , which used context menu from WP8 toolkit . So each item in this datatemplate has a button (to open context menu) and context menu also has click events

So when I use this datatemplate for my LongListSelectors in app, how can I call these click events?

<DataTemplate>
        <Grid>
            <Grid.ColumnDefenitions>.........
            ......
            <TextBlock Text="{Binding Name}" TextWrapping="Wrap" Grid.Column="0"/>
            <TextBlock Text="{Binding Artist}" TextWrapping="Wrap" Grid.Column="1"/>
            <Button Click="openContext" Grid.Column="2"/>
                <toolkit:ContextMenuService.ContextMenu>
                    <toolkit:ContextMenu IsZoomEnabled="false">
                        <toolkit:MenuItem Header="Download" 
                                          Click="context_download" />
                        <toolkit:MenuItem Header="Add to later" 
                                          Click="context_later" />
                        .......
                    </toolkit:ContextMenu>
                </toolkit:ContextMenuService.ContextMenu>
            </TextBlock>
        </StackPanel>
    </DataTemplate>

Solution

  • Context menu should be declared inside the element and not separately. Declare your context menu inside your button like below code and remove the click event in your button.

    Whenever you declare context menu in your button, it works automatically (opens context menu) without any click event.

    <Button Content="ContextMenu">
                <toolkit:ContextMenuService.ContextMenu>
                    <toolkit:ContextMenu IsZoomEnabled="False">
                        <toolkit:MenuItem Header="Download" Click="context_download"/>
                        <toolkit:MenuItem Header="Add to later" Click="context_later"/>
                    </toolkit:ContextMenu>
                </toolkit:ContextMenuService.ContextMenu>
            </Button>