C# 关注ListView之后的文本框';选择已更改';事件

C# 关注ListView之后的文本框';选择已更改';事件,c#,wpf,listview,focus,C#,Wpf,Listview,Focus,我希望在从列表视图控件中选择一个项目后,在文本框上设置焦点,但我似乎无法从我的“SelectionChanged”事件方法FocusContextBox()中将其设置为焦点 由于某些原因,ListView在进行选择后始终具有焦点 我创建了一个Visual Studio应用程序来演示此问题: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/x

我希望在从
列表视图
控件中选择一个项目后,在
文本框
上设置焦点,但我似乎无法从我的“SelectionChanged”事件方法
FocusContextBox()
中将其设置为焦点

由于某些原因,
ListView
在进行选择后始终具有焦点

我创建了一个Visual Studio应用程序来演示此问题:

<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>
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();
        }
    }
}

如何使焦点从“SelectionChanged”事件方法中返回到
文本框

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>
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();
        }
    }
}

MainWindow.xaml.cs:

<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>
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();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间WpfApplication1
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
//测试单词THIS和Focus()的选择是否有效
text_box1.SelectionStart=68;
text_box1.SelectionLength=4;
text_box1.Focus();
}
private void FocusContextBox(对象发送者,选择ChangedEventArgs e)
{
Show(“使用列表视图选择时激发的FocusContextBox”);
text_box1.SelectionStart=68;
text_box1.SelectionLength=4;
text_box1.Focus();
}
}
}

我认为发生这种情况是因为
列表视图未完成更改选择,因此您在
SelectionChanged
事件处理程序中切换焦点不起作用,因为在您更改焦点后,
列表视图将其更改回来,以完成其选择更改流程

如果您在
FocusOnTextBox
函数中放置一个断点并查看调用堆栈,您将看到您已经深入到了堆栈中,并且在执行
FocusOnTextBox
函数后,
ListView
将执行很多操作。我想它将要做的一件事是在
列表视图中设置所选项目的焦点

如果在
列表视图
完成对当前选择的更改后更改焦点,则应能正常工作。例如,在
MouseLeftButtonUp
中更改它以切换焦点似乎是可行的:

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


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

+1不错!这就是它,我只是在本地摆弄它,无法让它设置SelectionChanged事件的焦点;也没有办法取消活动,也没有活动预览;您的解决方案非常优雅,因为当用户单击以更改选择时,您可以将焦点更改附加到内部列表视图事件触发顺序的末尾。当我处理这个问题时,我检查了你的解决方案,它确实非常好地工作!谢谢@DavidKhaykin。我在WPF中花了很多时间来关注焦点,它会变得非常毛茸茸的。即使知道为什么会发生这种情况,我也在努力解释清楚,所以希望这是有意义的。