在ListView'SelectionChanged'事件之后,专注于TextBox [英] Focus on TextBox after ListView 'SelectionChanged' Event

查看:108
本文介绍了在ListView'SelectionChanged'事件之后,专注于TextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在从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会将其更改回以完成其选择更改流程.

如果在FocusOnTextBox函数中放置一个断点并查看调用堆栈",您会发现自己已经很深了,并且ListViewFocusOnTextBox函数执行.我想它要做的一件事就是将焦点放在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.

I wish to set focus on a TextBox after an item is selected from a ListView control, but I can't seem to get it to focus from within my 'SelectionChanged' event method FocusOnTextBox().

For some reason the ListView always has focus after a selection is made.

I have created a Visual Studio application to demonstrate this problem:

http://maq.co/focusapp.zip

How can I get the focus to return to the TextBox from within my 'SelectionChanged' event method?

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

解决方案

I believe this happens because the ListView is not finished changing the selection, and so you switching focus from within the SelectionChanged event handler doesn't stick because after you've changed focus, the ListView changes it back in order to complete its selection change flow.

If you put a breakpoint in the FocusOnTextBox function and look at the Call Stack, you'll see that you're pretty deep into the stack, and there is a lot that the ListView will do after your FocusOnTextBox function executes. I imagine one of the things it will do is set focus to the selected item in the ListView.

If you change it so that you're shifting focus after the ListView has completed changing the current selection, it should work. For example, changing it so you switch focus in the MouseLeftButtonUp seems to work:

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

And then you'll need to change the FocusOnTextBox event handler definition to use MouseEventArgs rather than SelectionChangedEventArgs as well.

这篇关于在ListView'SelectionChanged'事件之后,专注于TextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆