Windows phone 7 多重选择问题wp7列表框

Windows phone 7 多重选择问题wp7列表框,windows-phone-7,Windows Phone 7,在我的应用程序中,我用一个列表框实现了一个枢轴。在这种情况下,选择列表框项目将导航到另一个页面。在同一个列表框项目中,我还实现了一个上下文菜单,用于删除所选列表项目。在某些情况下,它工作得很好。这里我的问题是,在某些情况下,当按住listbox项时,上下文菜单出现,并显示delete选项。然后它导航到另一个页面,上下文菜单弹出窗口从不隐藏。任何人请帮我解决这个问题 我在这里附上我的代码片段:- <DataTemplate x:Key="GroupLoadedTemplate">

在我的应用程序中,我用一个列表框实现了一个枢轴。在这种情况下,选择列表框项目将导航到另一个页面。在同一个列表框项目中,我还实现了一个上下文菜单,用于删除所选列表项目。在某些情况下,它工作得很好。这里我的问题是,在某些情况下,当按住listbox项时,上下文菜单出现,并显示delete选项。然后它导航到另一个页面,上下文菜单弹出窗口从不隐藏。任何人请帮我解决这个问题

我在这里附上我的代码片段:-

    <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>

不幸的是,您的代码片段无法轻松地重新创建代码,因为它绑定到了您尚未指定的对象

是否存在不将ContextMenu应用于整个ListBoxItem的原因?这样做,我看不出有什么问题

我假设您正在SelectionChanged上导航。您可能希望添加一个
点击
手势(从工具箱中),并在该手势上导航。

我假设问题在于,当contextmenu开始显示时,选择被设置/更改。

不幸的是,您的代码片段无法轻松地重新创建代码,因为它绑定到了您未指定的对象

是否存在不将ContextMenu应用于整个ListBoxItem的原因?这样做,我看不出有什么问题

我假设您正在SelectionChanged上导航。您可能希望添加一个
点击
手势(从工具箱中),并在该手势上导航。

我假设问题在于,当contextmenu开始显示时,选择被设置/更改。

我有一个可能的解决方案

自定义ItemContainerStyle在ContentControl周围添加StackLayout或网格或其他内容。然后修改现有状态以显示或隐藏复选框(如果需要)。添加属性以绑定未与数据一起存储的复选框,并将{Binding YourNewProperty}绑定到复选框XAML

现在,您可以通过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}"/>

您还可以在列表框项目的循环中使用以下命令直接访问复选框。我认为使用绑定技术很简单

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()); 
                }
 }
foreach(列表框1.Items中的var loopItem)
{
ListBoxItemToCheck=listBox1.ItemContainerGenerator.ContainerFromItem(loopItem)作为ListBoxItem;
//用于查找复选框控件的代码
//查找该列表项的ContentPresenter..[调用FindVisualChild方法]
ContentPresenter ContentPresenterRobj=FindVisualChild(itemToCheck);
//在该ContentPresenter的DataTemplate上调用FindName
DataTemplate DataTemplateObj=ContentPresenterObj.ContentTemplate;
复选框Chk=(复选框)DataTemplateObj.FindName(“ChkList”,contentpresenterbj);
//获取选中的复选框项。
如果(Chk.IsChecked==true)
{
Show(Chk.Content.ToString().Trim());
}
}

我有一个可能的解决方案

自定义ItemContainerStyle在ContentControl周围添加StackLayout或网格或其他内容。然后修改现有状态以显示或隐藏复选框(如果需要)。添加属性以绑定未与数据一起存储的复选框,并将{Binding YourNewProperty}绑定到复选框XAML

现在,您可以通过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}"/>

您还可以在列表框项目的循环中使用以下命令直接访问复选框。我认为使用绑定技术很简单

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()); 
                }
 }
foreach(列表框1.Items中的var loopItem)
{
ListBoxItemToCheck=listBox1.ItemContainerGenerator.ContainerFromItem(loopItem)作为ListBoxItem;
//用于查找复选框控件的代码
//查找该列表项的ContentPresenter..[调用FindVisualChild方法]
ContentPresenter ContentPresenterRobj=FindVisualChild(itemToCheck);
//在该ContentPresenter的DataTemplate上调用FindName
DataTemplate DataTemplateObj=ContentPresenterObj.ContentTemplate;
复选框Chk=(复选框)DataTemplateObj.FindName(“ChkList”,contentpresenterbj);
//获取选中的复选框项。
如果(Chk.IsChecked==true)
{
Show(Chk.Content.ToString().Trim());
}
}