Search code examples
c#windowsxamlwinui-3winui

With WinUI 3 and C#, how do I make ListView items not selectable (IsHitTestVisible="False") but still make CheckBox controls checkable?


I have the following XAML code:

<ListView x:Name="filterListView">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="35" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <CheckBox Grid.Column="0" IsChecked="{Binding SubIsSelected, Mode=TwoWay}" />
                <TextBlock Grid.Column="1" Text="{Binding SubFilterName}" />
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

This code allows me to select ListView items like this:

enter image description here

What I want is for only the CheckBox to be checkable, not the actual rows selectable, so it would look like this all the time no matter where I click:

enter image description here

I have tried adding the following changes but it just makes nothing selectable/clickable at all:

<ListView IsHitTestVisible="False" x:Name="filterListView" >

<CheckBox IsHitTestVisible="True" Grid.Column="0" IsChecked="{Binding SubIsSelected, Mode=TwoWay}" />

Anyone have an ideas on what I can do to make this work how I'd like? CheckBox's checkable, everything else not selectable.


Solution

  • You can set the SelectionMode to None.