Listview禁用项目单击

时间:2013-07-07 19:21:51

标签: xaml windows-store-apps

我正在开发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>

我想禁用listview上的项目选择。所以它仅供查看,用户无法选择/点击listview中的任何内容。我怎么能做到这一点?我的问候......

P.S。 我将IsItemClickEnabled =“False”添加到listview行:

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

但它没有改变任何东西,仍然可以点击。

2 个答案:

答案 0 :(得分:9)

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

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

另外,根据您的需要,您可能仍然需要IsItemClickEnabled="False"

答案 1 :(得分:0)

我发现你需要修改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>