none
How to make a ListView respond to typing a single character? RRS feed

  • Question

  • I am sure this can be done, but I've forgotten how to do it. I've got a ListView where declared the item template to use a grid. The data returned is sorted by program name. If this were in a simple combobox where the program name was all that was in the combobox, I know that the user to just press a single letter to advance the combobox listing to that letter. Like if the user wanted to find a program name starting with the letter "T", they would just press the "t" key. I'm sure that the same thing can be done with a ListView, but can't remember how to do it at the moment.

    Here's the XAML, so you can see what I've done:

    <ListView
    	x:Name="ProgramsListView"
    	ItemsSource="{Binding Programs, Mode=TwoWay}"
    	ScrollViewer.VerticalScrollBarVisibility="Auto"
    	SelectedItem="{Binding SelectedProgram}">
    	<ListView.ItemTemplate>
    		<DataTemplate>
    			<Grid>
    				<Grid.ColumnDefinitions>
    					<ColumnDefinition Width="300" />
    					<ColumnDefinition Width="*" />
    				</Grid.ColumnDefinitions>
    				<Grid.RowDefinitions>
    					<RowDefinition Height="Auto" />
    					<RowDefinition Height="Auto" />
    					<RowDefinition Height="20" />
    				</Grid.RowDefinitions>
    				<Grid>
    					<Grid.ColumnDefinitions>
    						<ColumnDefinition Width="Auto" />
    						<ColumnDefinition Width="*" />
    					</Grid.ColumnDefinitions>
    					<TextBlock Margin="0,0,3,0">Program Name:</TextBlock>
    					<TextBlock
    						Grid.Column="1"
    						FontWeight="Bold"
    						Text="{Binding ProgramName}"
    						TextWrapping="Wrap" />
    				</Grid>
    				<StackPanel Grid.Column="1" Orientation="Horizontal">
    					<TextBlock Margin="10,0,3,0">Program Abbreviation:</TextBlock>
    					<TextBlock FontWeight="Bold" Text="{Binding ProgramAbbrev}" />
    				</StackPanel>
    				<StackPanel Grid.Row="1" Orientation="Horizontal">
    					<TextBlock Margin="0,0,3,0">Inactive:</TextBlock>
    					<TextBlock FontWeight="Bold" Text="{Binding Inactive}" />
    				</StackPanel>
    				<StackPanel
    					Grid.Row="1"
    					Grid.Column="1"
    					Orientation="Horizontal">
    					<TextBlock Margin="10,0,3,0">340B:</TextBlock>
    					<TextBlock FontWeight="Bold" Text="{Binding Is340B_YN}" />
    				</StackPanel>
    			</Grid>
    		</DataTemplate>
    	</ListView.ItemTemplate>
    </ListView>
    


    Rod

    Tuesday, October 8, 2019 9:13 PM

All replies

  • Deleted
    Tuesday, October 8, 2019 9:53 PM
  • Hmmm. That looks like it would solve my problem, but I remember it being somewhat simpler. Now I'm wondering if I should have chosen a different control, like a ListBox instead of a ListView?

    Rod

    Wednesday, October 9, 2019 1:24 PM
  • Deleted
    Wednesday, October 9, 2019 6:29 PM
  • @Rod at work, there is a SearchText (or very similar property) property on the Listview, and you can connect the text from Keyboard entries (e.g. in a TextBox) to it

    noorbakhsh حميد نوربخش

    Wednesday, October 9, 2019 7:19 PM
  • Deleted
    Wednesday, October 9, 2019 8:52 PM
  • @Rod at work, there is a SearchText (or very similar property) property on the Listview, and you can connect the text from Keyboard entries (e.g. in a TextBox) to it

    noorbakhsh حميد نوربخش

    Are you suggesting that I have a textbox that works in conjunction with the ListView?

    Rod

    Thursday, October 10, 2019 2:42 PM
  • @Rod at Work,

    That is up to you. If you are going to get a sequence of keys, you need to store them somewhere. You can either put a timer and if the keydowns come fast enough, put the characters together, OR have a 'filter' textbox and have user type in there and bind it's content to the SearchText of the ListView. I guess you have to decide how to handle that since you know the requirements. Either way you can have the TextBox (One can get populated from ListView keydowns and have visiblity = collapsed, so you can still use it's Text for binding)

    I hope I am making sense and you are following me.

    I just looked up in my code:

    <ListView TextSearch.TextPath="YourDesiredProperty" ...

    for your case I think:

    <ListView TextSearch.TextPath="ProgramName"...


    I think I just have the TextBox for user as part of requirement, it works without it by just using KeyDown and maps it to my property for filtering.


    noorbakhsh حميد نوربخش






    • Edited by noorbakhsh Thursday, October 10, 2019 4:00 PM
    Thursday, October 10, 2019 3:30 PM