In my WinUI3 app I have a ListView control that illustrates the 'current' item in another part of the app. For visual consistency this is done by binding to the SelectedItem
property of the ListView. I would like to stop the user from being able to select other items in the ListView, for example by clicking them.
What I've tried so far:
- Setting
IsItemClickEnabled="False"
on the ListView. No apparent effect. - Setting
SelectionMode="None"
on the ListView. Disables the display of the current item. - Placing a transparent control in front of the ListView. Stops scrolling of the ListView.
- Editing the visual state of ListViewItem as per visc's answer here. Disables the display of the current item.
- Setting
IsHitTestVisible = false
on the item containers (as per mm8's answer below). Stops all interaction on the child controls e.g. scrolling of the child ListViews in the example code below.
Does anyone have any other suggestions?
Edit: The challenge seems to be blocking user selection without blocking all interaction. My ListView actually contains other listviews, like the code below. Crucially, I'd like to retain the ability for the user to (horizontally) scroll the child ListView items.
XAML:
<Grid RowDefinitions="Auto,*">
<TextBox x:Name="SelectedItemTextBox" Grid.Row="0" />
<ListView
Grid.Row="1"
ItemsSource="{x:Bind Items, Mode=OneWay}"
SelectedIndex="2">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:MyItem">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock
Grid.Row="0"
Margin="0,8,0,0"
Text="{x:Bind Name, Mode=OneWay}" />
<ListView
Grid.Row="1"
Margin="4,0,0,4"
ItemsSource="{x:Bind SubItems}"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollMode="Enabled"
ScrollViewer.VerticalScrollMode="Disabled"
SelectedIndex="2">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<TextBlock Text="{x:Bind}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
C#:
public sealed partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// Initially set the target outside the dialog
RootCanvas.Children.Add(TeachingTip);
TeachingTip.Target = DialogButton;
TeachingTip.IsOpen = true;
for (int i = 0; i < 10; i++)
{
MyItem item = new() { Name = i.ToString() };
for (int j = 0; j < 5; j++)
{
item.SubItems.Add($"Sub item {j + 1}");
}
Items.Add(item);
}
}
public ObservableCollection<MyItem> Items { get; } = new();
}
public class MyItem
{
public List<string> SubItems { get; } = new();
public string Name { get; set; }
}