XAML 列表视图禁用项目单击

vpfxa7rd  于 2023-05-11  发布在  其他
关注(0)|答案(2)|浏览(210)

我正在开发Windows应用商店应用程序,我有这样的XAML代码:

<Popup x:Name="Panel3" IsOpen="False" Grid.ColumnSpan="18" Grid.Column="13" Grid.Row="4" Grid.RowSpan="31">
    <StackPanel>
        <Rectangle  Width="765" Height="10" />
        <ListView x:Name="Person" Grid.ColumnSpan="18" Grid.Column="13" HorizontalAlignment="Left" Height="643" Grid.Row="4" Grid.RowSpan="31" VerticalAlignment="Top" Width="765" >
            <ListView.Background>
                <SolidColorBrush Color="#FF665920" Opacity="0.85"/>
            </ListView.Background>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding name}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackPanel>
</Popup>

我想在列表视图上禁用项目选择。所以它只用于查看,用户不能选择/单击列表视图中的任何内容。我怎么能让它发生呢?我的问候
P.S.我在listview行添加了IsItemClickEnabled=“False”:

<ListView x:Name="Person" Grid.ColumnSpan="18" Grid.Column="13" HorizontalAlignment="Left" Height="643" Grid.Row="4" Grid.RowSpan="31" VerticalAlignment="Top" Width="765" IsItemClickEnabled="False">

但它并没有改变什么,仍然可以点击。

knsnq2tg

knsnq2tg1#

您需要将SelectionMode属性设置为None以禁用ListView的项目选择:

<ListView x:Name="Person" SelectionMode="None" ... />

此外,您可能仍然需要IsItemClickEnabled="False",这取决于您的需要。

bq3bfh9z

bq3bfh9z2#

在具有高亮显示的列表视图项目动画的较新版本的Windows 10中
我发现你需要修改ListViewItem的视觉状态沿着设置SelectionMode="None"IsItemClickEnabled="False",如nemesv在他的回答中所说。

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <!-- here we are clearing the state behavior, 
                                     thus disabling the clickability of the ListViewItem -->
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="PointerOver" />
                                <VisualState x:Name="Pressed" />
                             </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Grid>
                            <ContentPresenter x:Name="ListViewItemContent" />
                        </Grid>
                   </Grid>
                </ControlTemplate>
            </Setter.Value>
       </Setter>
    </Style>
</ListView.ItemContainerStyle>

相关问题