Context menu selected item wp7

被刻印的时光 ゝ 提交于 2019-12-24 12:32:45

问题


I have a Listbox. Each item has Context menu.IfI simply hold on item and do work with it, it not selected and I get error.If I for the first select item and than do work, all is ok.How I can select item on hold gesture?

              <DataTemplate>
                <Grid Margin="0,5">
                  <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                  </Grid.ColumnDefinitions>
                  <toolkit:ContextMenuService.ContextMenu>
                    <toolkit:ContextMenu IsEnabled="{Binding uid, Converter={StaticResource CanDelete}}"  IsZoomEnabled="True" x:Name="databoundMenu">
                      <toolkit:MenuItem Header="Удалить"  Click="deleteComment"/>
                    </toolkit:ContextMenu>
                  </toolkit:ContextMenuService.ContextMenu>
.....
                </Grid>
              </DataTemplate>

c#

var it = this.comm_box.SelectedItem as Comments;

回答1:


The ContextMenuService for obvious reasons, doesn't invoke the SelectionChanged event, and doesn't set the SelectedItem, since this would mean that ContextMenus with options such as "Remove" would bug out.

What I think is your problem is that you're not checking if the SelectedItem is actually set, before doing the work on it.

Instead you should validate that the SelectedItem is not null, before doing any work with it.




回答2:


You need to travel up the VisualTree to get the FrameworkELement in the ListBox. This should be done in your click handler.

private void deleteComment(object sender, RoutedEventArgs e)
{
    var menuItem = sender as MenuItem;
    var fe =VisualTreeHelper.GetParent(menuItem) as FrameworkElement;
    var comment = fe.DataContext as Comments;
    // deleteComment

}


来源:https://stackoverflow.com/questions/6860569/context-menu-selected-item-wp7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!