How would you change the behavior of ListView so that the selection of the record happens immediately, by the focus of the mouse or the navigation keys?

I do not think where it should be, what we have:

<ListView x:Name="listText" Grid.Row="0" Grid.Column="0" Margin="10,2,0,2" Height="250" BorderThickness="1" SelectedIndex="-1" ItemsSource="{local:SettingBinding TextToSend}" Style="{StaticResource VirtualisedMetroListView}"> <ListView.View> <GridView> <GridViewColumn Width="850"> <GridViewColumn.CellTemplate> <DataTemplate> <TextBox Text="{Binding Mode=OneTime}" BorderThickness="0" Background="Transparent" IsReadOnly="True" MouseDoubleClick="txt_MouseDoubleClick" KeyDown="txt_KeyDown" CtrlMh:TextBoxHelper.Watermark="Enter new text" CtrlMh:TextBoxHelper.WatermarkWrapping="WrapWithOverflow" SpellCheck.IsEnabled="{local:SettingBinding IsSpellCheck}" Width="{Binding ActualWidth, ElementName=listText, Mode=OneWay}"/> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> </ListView> 

UPD Possible solution:

 <ListView x:Name="listText" Grid.Row="0" Grid.Column="0" Margin="10,2,0,2" Height="250" BorderThickness="1" SelectedIndex="-1" ItemsSource="{local:SettingBinding TextToSend}" Style="{StaticResource VirtualisedMetroListView}"> <ListView.ItemContainerStyle> <Style TargetType="{x:Type ListViewItem}"> <Style.Triggers> <Trigger Property="IsKeyboardFocusWithin" Value="True"> <Setter Property="IsSelected" Value="True" /> </Trigger> </Style.Triggers> </Style> </ListView.ItemContainerStyle> <ListView.View> <GridView> <GridViewColumn Width="820"> <GridViewColumn.CellTemplate> <DataTemplate> <TextBox Text="{Binding Mode=OneTime}" BorderThickness="0" Background="Transparent" IsReadOnly="True" MouseDoubleClick="txt_MouseDoubleClick" KeyDown="txt_KeyDown" CtrlMh:TextBoxHelper.Watermark="Enter new text" CtrlMh:TextBoxHelper.WatermarkWrapping="WrapWithOverflow" SpellCheck.IsEnabled="{local:SettingBinding IsSpellCheck}" Language="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}},Path=DataContext.CultureCurent}" Width="{Binding ActualWidth, ElementName=listChatText, Mode=OneWay}"> <SpellCheck.CustomDictionaries> <CtrlSys:Uri>pack://application:,,,/spellwords.lex</CtrlSys:Uri> </SpellCheck.CustomDictionaries> </TextBox> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> </ListView> 
  • one
    I do not understand the question, do you want ListViewItem be selected when you hover the mouse or what? - Andrey NOP
  • Yes exactly. I have already implemented, not quite as planned, but will, with the help of triggers. If you propose a solution, I will be grateful. Now add how I decided it. - NewView

1 answer 1

For example, using a trigger:

 <ListBox ItemsSource="{Binding Items}"> <ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="IsSelected" Value="True"/> </Trigger> </Style.Triggers> </Style> </ListBox.ItemContainerStyle> </ListBox> 

enter image description here