开发者

Multiple selection problem wp7 listbox

开发者 https://www.devze.com 2023-03-03 03:50 出处:网络
In my app i implemented a pivot with a list box. In that selecting a listbox item will navigates to another page. Also in the same list box item i implemented a context menu for deleting the selected

In my app i implemented a pivot with a list box. In that selecting a listbox item will navigates to another page. Also in the same list box item i implemented a context menu for deleting the selected List item. Some cases it works perfectly. Here my issue is that, in some case while holding on listbox item the context menu comes and delete option appears. and then it navigates to the other page and the context menu popup never hides. Any one please help me to solve this issu开发者_如何学Pythone.

Here I am attaching my code snippet :-

    <DataTemplate x:Key="GroupLoadedTemplate">
        <Grid Height="120" Width="480" VerticalAlignment="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="110"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Border Height="105" Width="110" BorderBrush="White" Grid.Column="0" BorderThickness="2">
                <Image delay:LowProfileImageLoader.UriSource="{Binding Path=Avatar}" Source="/Image/default-thumb-groups.png"/>
            </Border>
            <Grid Grid.Column="1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="4"/>
                    <RowDefinition Height="35"/>
                    <RowDefinition Height="50"/>

                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="10" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <TextBlock Text="{Binding Path=Name,Mode=TwoWay}"  Grid.Column="1" Grid.Row="1" FontFamily="Segoe WP Light" FontSize="30" Foreground="{StaticResource PhoneForegroundBrush}" TextWrapping="Wrap"/>
                <TextBlock Text="{Binding Path=Members,Mode=TwoWay}"   Grid.Column="2"  Grid.Row="2" FontFamily="Segoe WP Light" HorizontalAlignment="Left"  FontSize="20" Opacity="0.91" Foreground="{StaticResource PhoneForegroundBrush}" TextWrapping="Wrap"/>

                <toolkit:ContextMenuService.ContextMenu>
                    <toolkit:ContextMenu Name="DeleteGroup" Tag="{Binding Nid,Mode=TwoWay}" Visibility="{Binding ElementName=GroupList,  Path=DataContext.DeleteStatus,Mode=TwoWay, Converter={StaticResource booleanToVisibility}}" IsZoomEnabled="False">
                        <toolkit:MenuItem Header="delete group">
                            <Interactivity:Interaction.Triggers>
                                <Interactivity:EventTrigger EventName="Click">
                                    <Command:EventToCommand Command="{Binding ElementName=GroupList, Path=DataContext.DeleteCommand,Mode=TwoWay}"  CommandParameter="{Binding ElementName=DeleteGroup}" PassEventArgsToCommand="True"/>
                                </Interactivity:EventTrigger>
                            </Interactivity:Interaction.Triggers>
                        </toolkit:MenuItem>
                    </toolkit:ContextMenu>
                </toolkit:ContextMenuService.ContextMenu>


            </Grid>
        </Grid>
    </DataTemplate>


Unfortunately your code snippet doesn't make it easy to recreate your code as it is bound to objects you haven't specified.

Is there a reason you are not applying the ContextMenu to the entire ListBoxItem? I've not seen a problem when doing it that way.

I assume that you're navigating on SelectionChanged. You may want to add a Tap gesture (from the toolkit) and navigate on that instead.
I assume that the problem is that the selection is set/changed when the contextmenu starts to be displayed.


I have a possible solution.

Customize the ItemContainerStyle add a StackLayout or Grid or what ever around the ContentControl. Then modify the existing states to show or hide your checkboxes (if needed). Add a property to bind the checkboxes that is not stored with your data, and {Binding YourNewProperty} to the checkbox XAML.

You will now be able to control and read your selected checkboxes via your datacontext.

<CheckBox Content="CheckBox" Margin="0,0,50,0" VerticalAlignment="Top" d:LayoutOverrides="Width" IsChecked="{Binding YourNewProperty}"/>
<ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}"/>

You could also access the checkboxes directly using the following in a loop of your listbox items. I think using the Binding technique is simple.

foreach (var loopItem in listBox1.Items)
{
            ListBoxItem itemToCheck = listBox1.ItemContainerGenerator.ContainerFromItem(loopItem) as ListBoxItem;

            // code to find the check box control
            // find a ContentPresenter of that list item.. [Call FindVisualChild Method]
                ContentPresenter ContentPresenterObj = FindVisualChild<ContentPresenter>(itemToCheck);

                // call FindName on the DataTemplate of that ContentPresenter
                DataTemplate DataTemplateObj = ContentPresenterObj.ContentTemplate;
                CheckBox Chk = (CheckBox)DataTemplateObj.FindName("ChkList", ContentPresenterObj);

                // get a selected checkbox items.
                if (Chk.IsChecked == true)
                {
                    MessageBox.Show(Chk.Content.ToString().Trim()); 
                }
 }
0

精彩评论

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

关注公众号