如何防止在Windows Universal app

时间:2017-03-24 06:18:58

标签: c# gridview uwp-xaml

在Windows Universal app中,我有一个数据透视表,每个标签内容都有gridview,gridview项目是多选模式,

如果任何一个项目一旦选中(选中),我想要的是,那么它无法取消选择(取消选中)

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

2 个答案:

答案 0 :(得分:1)

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

  

调用SelectRange(ItemIndexRange)时,无论原始选择状态如何,都会选择指定范围内的所有项目。您可以使用ItemIndexRange选择集合中的所有项目,其中FirstIndex值为0,Length值等于集合中的项目数。

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

我们可以使用ItemClick来获取点击的项目。然后,我们可以添加SelectionChanged事件,并将所点击的项目的编号设置为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));
}

答案 1 :(得分:0)

如果我们想保留选中的项目,请使用以下方法。

 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);
       }