【问题标题】:Focus on TextBox after ListView 'SelectionChanged' Event在 ListView 'SelectionChanged' 事件之后关注文本框
【发布时间】:2013-03-06 18:34:00
【问题描述】:

我希望在从 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();
        }
    }
}

【问题讨论】:

    标签: c# wpf listview focus


    【解决方案1】:

    我相信发生这种情况是因为 ListView 没有完成更改选择,因此您从 SelectionChanged 事件处理程序中切换焦点不会粘住,因为在您更改焦点后,ListView 会更改它返回以完成其选择更改流程。

    如果你在FocusOnTextBox 函数中放置一个断点并查看调用堆栈,你会发现你已经深入堆栈了,ListView 在你的@ 之后会做很多事情987654326@ 函数执行。我想它会做的其中一件事是将焦点设置到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

    【讨论】:

    • +1 不错!就是这样,我只是在本地摆弄它,无法将焦点集中在 SelectionChanged 事件上;也没有办法取消活动,也没有活动预览;您的解决方案很优雅,因为当用户单击更改选择时,您将焦点更改附加到内部列表视图事件触发顺序的末尾。当我摆弄这个时,我检查了你的解决方案,它确实工作得很好!
    • 谢谢@DavidKhaykin。我在 WPF 中与 Focus 进行了很多角力,它会变得非常棘手。即使知道为什么在这种情况下,我也很难清楚地解释它,所以希望它是有道理的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多