开发者

Context menu selected item wp7

开发者 https://www.devze.com 2023-03-23 08:38 出处:网络
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 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;


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.


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

}
0

精彩评论

暂无评论...
验证码 换一张
取 消