如何防止在 windows 通用应用程序中取消选中(取消选择)gridview 项目

How to prevent uncheck (unselect) gridview item in windows Universal app

在 Windows 通用应用程序中,我有一个枢轴 table,每个选项卡内容都有 gridview,gridview 项目是多 select 模式,

我想要的是,如果任何一项一旦选中(selected),就无法取消select(取消选中)

<Grid DataContext="{Binding Path=Value}">
                            <GridView x:Name="categoryItemsGV" 
                                Margin="5,5,0,0"
                                SizeChanged="categoryItemsGV_SizeChanged"
                                IsItemClickEnabled="True"
                                ItemClick="categoryItemsGV_ItemClick"
                                SelectionMode="Single" 
                                ItemsSource="{Binding}">
                                <GridView.ItemContainerStyle>
                                    <Style TargetType="GridViewItem">
                                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                                        <!--<Setter Property="VerticalContentAlignment" Value="Center"/>-->
                                    </Style>
                                </GridView.ItemContainerStyle>
                                <GridView.ItemTemplate>
                                    <DataTemplate>
                                        <Grid Width="195" Height="43" Margin="3">
                                            <StackPanel Width="193" Height="40"  Background="Gray" Opacity="0.5" HorizontalAlignment="Right" VerticalAlignment="Bottom" />
                                            <StackPanel Orientation="Horizontal" Width="193" Height="40" Padding="7,7,0,0" Background="#FDFCC2"  HorizontalAlignment="Left" VerticalAlignment="Top">
                                                <TextBlock  Text="{Binding ProductOptionLineName}" FontSize="18" MaxLines="1" TextTrimming="CharacterEllipsis"   Visibility="{Binding Converter={StaticResource langToVisibilityConverter}, ConverterParameter='CH', Mode=OneWay}">
                                                </TextBlock>
                                                <TextBlock  Text="{Binding ProductOptionLineNameEn}" FontSize="18" MaxLines="1" TextTrimming="CharacterEllipsis"  Visibility="{Binding Converter={StaticResource langToVisibilityConverter}, ConverterParameter='EN', Mode=OneWay}"/>
                                                <TextBlock Text="{Binding ExtraPriceString}" FontSize="18" Margin="2,0,0,0"></TextBlock>
                                            </StackPanel>
                                        </Grid>
                                    </DataTemplate>
                                </GridView.ItemTemplate>
                            </GridView>
                        </Grid>


  private async void categoryItemsGV_ItemClick(object sender, ItemClickEventArgs e)
        {

                var item = e.ClickedItem as ProductOptionLineModel;
       }

我们可以使用 SelectRange(ItemIndexRange) 方法来选择 ItemIndexRange 描述的项目块。

When you call SelectRange(ItemIndexRange), all items in the specified range are selected, regardless of their original selection state. You can select all items in a collection by using an ItemIndexRange with a FirstIndex value of 0 and a Length value equal to the number of items in the collection.

有关详细信息,请参阅 Remarks of the SelectRange(ItemIndexRange)

我们可以使用ItemClick 来获取被点击的项目。然后我们可以添加SelectionChanged事件,将点击的item的数量设置到SelectRange方法中

例如:

private int number;

private void categoryItemsGV_ItemClick(object sender, ItemClickEventArgs e)
{
    var item = e.ClickedItem as ProductOptionLineModel;
    number = ProductOptionLineModels.IndexOf(item);
}

private void categoryItemsGV_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    categoryItemsGV.SelectRange(new ItemIndexRange(number, 1));
}

如果我们想让项目保持选中状态,因为它已经被选中,请使用以下方法。

 private async void categoryItemsGV_ItemClick(object sender, ItemClickEventArgs e)
        {

                var item = e.ClickedItem as ProductOptionLineModel;
                        GridView gv = sender as GridView;
                        gv.SelectedItems.Remove(item);
       }

并且如果我们希望在单击时保留项目取消选择(防止取消选择)以进行验证。使用以下方法。

private async void categoryItemsGV_ItemClick(object sender, ItemClickEventArgs e)
        {

                var item = e.ClickedItem as ProductOptionLineModel;
                            await   _viewModel.DialogService("FirstRequireMinOption", "", true);
                            GridView gv = sender as GridView;
                            gv.SelectedItems.Add(item);
       }