关注ListView 'SelectionChanged'事件

本文关键字:事件 SelectionChanged ListView 关注 | 更新日期: 2023-09-27 18:02:49

我希望在从ListView控件中选择项目后将焦点设置在TextBox上,但我似乎无法从我的'SelectionChanged'事件方法FocusOnTextBox()中获得焦点。

由于某些原因,ListView在选择后总是有焦点。

我已经创建了一个Visual Studio应用程序来演示这个问题: http://maq.co/focusapp.zip

我怎样才能让焦点从我的"SelectionChanged"事件方法中返回到TextBox ?

MainWindow.xaml:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <ListView Name="list_view1" Grid.Column="0" SelectionChanged="FocusOnTextBox">
            <ListViewItem Content="1" />
            <ListViewItem Content="2" />
            <ListViewItem Content="3" />
        </ListView>
        <TextBox Name="text_box1" Grid.Column="1" Text="When a selection is chosen from the left hand side ListView, I want THIS word to be selected and for the focus to change to this text box - this will show the selection to the user.&#x0a;&#x0a;However, right now it doesn't seem to work, the focus remains on the ListView regardless of it being set to Focus() in the FocusOnTextBox() method, which is fired on the 'SelectionChanged' event." TextWrapping="Wrap" />
    </Grid>
</Window>

MainWindow.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            // Test that the selection of the word THIS and the Focus() works
            text_box1.SelectionStart = 68;
            text_box1.SelectionLength = 4;
            text_box1.Focus();
        }
        private void FocusOnTextBox(object sender, SelectionChangedEventArgs e)
        {
            MessageBox.Show("FocusOnTextBox fired on selection with list view");
            text_box1.SelectionStart = 68;
            text_box1.SelectionLength = 4;
            text_box1.Focus();
        }
    }
}

关注ListView 'SelectionChanged'事件

我认为发生这种情况是因为ListView没有完成更改选择,因此您从SelectionChanged事件处理程序中切换焦点不会坚持,因为在您更改焦点之后,ListView将其更改回来以完成其选择更改流程。

如果你在FocusOnTextBox函数中放置一个断点并查看调用堆栈,你会发现你已经进入了堆栈的深处,并且在FocusOnTextBox函数执行之后,ListView将做很多事情。我想它会做的一件事是在ListView中将焦点设置为选定的项目。

如果你改变它,在ListView完成改变当前选择之后你转移焦点,它应该工作。例如,更改它,以便在MouseLeftButtonUp中切换焦点似乎有效:

<ListView Name="list_view1" Grid.Column="0" MouseLeftButtonUp="FocusOnTextBox">
    <ListViewItem Content="1" />
    <ListViewItem Content="2" />
    <ListViewItem Content="3" />
</ListView>

然后您需要更改FocusOnTextBox事件处理程序定义,以使用MouseEventArgs而不是SelectionChangedEventArgs