WPF列表视图失去焦点 [英] wpf listview lost the focus

查看:167
本文介绍了WPF列表视图失去焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF ListView控件的一些奇怪的行为。

I have some strange behavior of ListView control in WPF.

我们在C#开发应用。

我创建了一些项目的ListView和设置的SelectedIndex一些指数
其列表的中间。然后,我在对的ListView任何地方点击鼠标
然后导航与上/道兴键盘按键,一切都完美。但如果
我将焦点移到另一个控件,然后尝试将焦点设置回的ListView
控制编程,然后我发现了问题。

I created ListView with some items and set SelectedIndex to some index in the middle of its list. Then I clicked mouse at any place on the ListView and then navigate with Up/Dows keyboard keys, everything works perfectly. But If I move focus to another control and then tries to set focus back to ListView control programmatically, then I'm getting the problem.

ListView控件获得焦点,但在保险业监督€™米试图与导航
键盘,当前选择将被取消,第一个项目将是
选择和导航将从列表中的第一个项目开始。

The ListView gets the focus, but when I’m trying to navigate with the keyboard, current selection will be canceled and the first item will be selected and navigation will start from the first item in list.

我通过调用它。重点()方法将焦点设置到ListView控件。

I'm setting focus to ListView control by calling to its .Focus() method.

看起来甚至在重点ListView控件,但实际上没有任何项目在
对焦,即使SelectedIndex的有一些有效的值。

It looks like even ListView control in focus, but no items actually in focus, even if SelectedIndex has some valid values.

请帮帮我!我怎样才能将焦点设置到ListView控件以编程方式
prevent这样的行为?

Please, help me! How can I set focus to ListView control programmatically to prevent such behavior?

我发现,我的列表视图中选择指数为焦点项不同。
谁???

i find out that my listview selected index is different from the focused item . who ???

在selectedindex_changed事件中,我将选定的指标修复项目(5),但是当我preSS箭头键的项目已​​被更改。意味着选择指数固定为5,但目前的项目(与它周围的边界点显示)移动到方向键的位置。
反正我是将selectedItem固定到5,但我的scrollviews移动定位,目前的产品。

in selectedindex_changed event i set the selected index to the fix item (5) , but when i press arrow keys the item was changed . that mean the selected index is fixed to 5 but the current item(shown with a dot border around it ) moved to the arrow key location . anyway my selecteditem is fix to 5 , but my scrollviews moves to position that current item is .

这code为listView1_SelectionChanged:
listView1.SelectionMode = SelectionMode.Single;
            listView1.SelectedIndex = 5;

this code is in listView1_SelectionChanged: listView1.SelectionMode = SelectionMode.Single; listView1.SelectedIndex = 5;

我的ListView有一个修改的看法:
        
            
                
                            
                
            
        

my listview has a modified view :

    <l:PlainView x:Key="tileView"
            ItemTemplate="{StaticResource centralTile}"
            ItemHeight="120" ItemWidth="130" />

我不知道有关problom了。

i dont know about the problom any more .

关于

推荐答案

的ListView可能是由同一个问题的列表框呢,它可以独立的项目具有焦点受到影响。

ListView probably suffers from the same issue that ListBox does, in that it can have focus independently of its items.

当我想集中一个ListBox,我需要做的是这样的:

When I want to focus a ListBox, I need to do something like this:

listBox1.Focus();
if (listBox1.Items.Count > 0)
{
    var index = listBox1.SelectedIndex;
    if (index < 0) index = 0;

    var item = listBox1.ItemsContainerGenerator
                   .ContainerFromIndex(index) as ListBoxItem;
    item.Focus();
}

所以这个重点列表框,但如果ListBox中包含任何项目如果选择了一个它的重点不是选定的项目,或第一个项目。

So this focuses the ListBox, but then if the ListBox contains any items it focuses either the selected item, or the first item if one is selected.

我从来没有使用WPF中的ListView,但presumably你可以使用的ListView来代替ListBox和[ListViewItem的]代替ListBoxItem中的砍在一起类似的方法。

I've never used ListView in WPF, but presumably you could hack together a similar method using ListView instead of ListBox and [ListViewItem] in place of ListBoxItem.

这篇关于WPF列表视图失去焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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