Press Shift to select multiple boxes in WPF

Press Shift to select multiple boxes in WPF

Luffy11
Participant Participant
600 Views
3 Replies
Message 1 of 4

Press Shift to select multiple boxes in WPF

Luffy11
Participant
Participant

Hi everyone, I’m creating a form in WPF but I can’t press Shift to select multiple boxes though I have set the SelectionMode to Extended. Can you look at the XAML code below and tell me why?

 

```
<Window x:Class="Practice.CopyFilterCurrentModel.CopyFilterCurrentModelWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:local="clr-namespace:Practice.CopyFilterCurrentModel"
        mc:Ignorable="d" 
        Width="600"
        Height="550"
        ShowInTaskbar="True"
        WindowStartupLocation="CenterScreen"
        Title="Copy Filter Current Model"
        ResizeMode="CanResize">

    <Grid>
        <!-- Define rows in the Grid -->
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <!-- Source GroupBox -->
            <RowDefinition Height="*"/>
            <!-- Destination GroupBox -->
            <RowDefinition Height="Auto"/>
            <!-- Buttons -->
        </Grid.RowDefinitions>

        <!-- GroupBox with Source Header -->
        <GroupBox Header="Source" BorderBrush="Gray" VerticalAlignment="Stretch" Margin="10" Grid.Row="0">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                    <!-- Ensures ListView expands -->
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="150"/>
                    <!-- Label width -->
                    <ColumnDefinition Width="*"/>
                    <!-- ComboBox and ListView width -->
                </Grid.ColumnDefinitions>

                <!-- Source View ComboBox -->
                <Label Content="Source View: " Margin="10 0 0 0" VerticalAlignment="Center"/>
                <ComboBox x:Name="cbb_SourceView" Grid.Column="1" Margin="0 5 17 5" SelectionChanged="cbb_SourceView_SelectionChanged" HorizontalAlignment="Stretch"/>

                <!-- Filter ListView with ScrollViewer -->
                <Label Content="Filters: " Grid.Row="1" Margin="10 10 0 0" VerticalAlignment="Top"/>
                <ScrollViewer x:Name="sv_Filters" Grid.Column="1" Grid.Row="1" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Disabled" CanContentScroll="True">
                    <ListView x:Name="lv_Filters" 
                              SelectionMode="Extended" 
                              PreviewMouseWheel="ListBox_PreviewMouseWheel" 
                              MinHeight="200">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <CheckBox IsChecked="{Binding IsChecked}"
                                              VerticalAlignment="Center" 
                                              Margin="0,0,5,0"/>
                                    <TextBlock Text="{Binding}" VerticalAlignment="Center" IsHitTestVisible="False"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>

                </ScrollViewer>
            </Grid>
        </GroupBox>

        <!-- GroupBox with Destination Header-->
        <GroupBox Header="Destination" BorderBrush="Gray" VerticalAlignment="Stretch" Margin="10" Grid.Row="1">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="150"/>
                    <!-- Label width -->
                    <ColumnDefinition Width="*"/>
                    <!-- ComboBox and ListView width -->
                </Grid.ColumnDefinitions>

                <Label Content="Destination Views: " Margin="10 0 0 0" VerticalAlignment="Center"/>

                <ScrollViewer x:Name="sv_DestinationViews" Grid.Column="1" Grid.Row="1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" CanContentScroll="True">
                    <ListView x:Name="lv_DestinationViews" SelectionMode="Extended" PreviewMouseWheel="ListBox_PreviewMouseWheel" MinHeight="200">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <CheckBox IsChecked="{Binding IsChecked}" VerticalAlignment="Center" Margin="0,0,5,0"/>
                                    <TextBlock Text="{Binding}" VerticalAlignment="Center" IsHitTestVisible="False"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </ScrollViewer>
            </Grid>
        </GroupBox>

        <!-- OK and Cancel buttons -->
        <StackPanel Grid.Row="2" Margin="5,5,5,5" VerticalAlignment="Bottom" HorizontalAlignment="Right" Orientation="Horizontal">
            <Button x:Name="bt_Ok" Content="OK" Width="100" Click="bt_Ok_Click"/>
            <Button x:Name="bt_Cancel" Content="Cancel"  Width="100" Margin="5 0 0 0" Click="bt_Cancel_Click"/>
        </StackPanel>
    </Grid>
</Window>

```

 

 

0 Likes
601 Views
3 Replies
Replies (3)
Message 2 of 4

Mohamed_Arshad
Advisor
Advisor

Hi @Luffy11 

   

The issue you're experiencing is likely because the Shift key for multiple selection in WPF's ListView requires the ItemsSource to be bound to a collection and proper data bindings for selection tracking. Currently, the ListView seems to lack a proper binding that tracks individual item selection.

 

I have added a sample code snippet for ListView, Kindly Implement the snippet in your code.

 

Reference Snippet

 

 <!-- Filters -->
<ScrollViewer x:Name="sv_Filters" Grid.Column="1" Grid.Row="1" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Disabled" CanContentScroll="True">
                    <ListView x:Name="lv_Filters" 
                              ItemsSource="{Binding Filters}" 
                              SelectionMode="Extended"
                              MinHeight="200">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <CheckBox IsChecked="{Binding IsChecked}" VerticalAlignment="Center" Margin="0,0,5,0"/>
                                    <TextBlock Text="{Binding Name}" VerticalAlignment="Center" IsHitTestVisible="False"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </ScrollViewer>

 <!-- Destination Views -->
<ScrollViewer x:Name="sv_DestinationViews" Grid.Column="1" Grid.Row="1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" CanContentScroll="True">
                    <ListView x:Name="lv_DestinationViews" ItemsSource="{Binding DestinationViews}" SelectionMode="Extended" MinHeight="200">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <CheckBox IsChecked="{Binding IsChecked}" VerticalAlignment="Center" Margin="0,0,5,0"/>
                                    <TextBlock Text="{Binding Name}" VerticalAlignment="Center" IsHitTestVisible="False"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </ScrollViewer>

 

 

ViewModel Implementation

 

public class FilterItem : INotifyPropertyChanged
    {
        private bool _isChecked;
        public string Name { get; set; }

        public bool IsChecked
        {
            get { return _isChecked; }
            set
            {
                _isChecked = value;
                OnPropertyChanged(nameof(IsChecked));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class CopyFilterCurrentModelWindowViewModel
    {
        public ObservableCollection<FilterItem> Filters { get; set; }
        public ObservableCollection<FilterItem> DestinationViews { get; set; }

        public CopyFilterCurrentModelWindowViewModel()
        {
            Filters = new ObservableCollection<FilterItem>
            {
                new FilterItem { Name = "Filter 1", IsChecked = false },
                new FilterItem { Name = "Filter 2", IsChecked = false },
                new FilterItem { Name = "Filter 3", IsChecked = false }
            };

            DestinationViews = new ObservableCollection<FilterItem>
            {
                new FilterItem { Name = "Destination 1", IsChecked = false },
                new FilterItem { Name = "Destination 2", IsChecked = false },
                new FilterItem { Name = "Destination 3", IsChecked = false }
            };
        }
    }

 

 

View Backend

 

            InitializeComponent();
            DataContext = new CopyFilterCurrentModelWindowViewModel();

 

 

Reference Video

 

Hope this will Helps 🙂

 


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes
Message 3 of 4

Luffy11
Participant
Participant

hi @Mohamed_Arshad , sorry for the unclear. I want to tick the boxes with Shift key as in the video below by not just selecting them (because I already did it).

 

0 Likes
Message 4 of 4

Luffy11
Participant
Participant

Sorry I made a mistake, it's not a solution so I deleted the response. Haha

0 Likes