KeyBoard.TabNavigation =“包含" + Canvas.ZIndex ="2"在WPF中的listView上给出奇怪的行为? [英] KeyBoard.TabNavigation=“Contained” + Canvas.ZIndex=“2” give strange behavior on listView in WPF?

查看:44
本文介绍了KeyBoard.TabNavigation =“包含" + Canvas.ZIndex ="2"在WPF中的listView上给出奇怪的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人曾经偶然发现过这个,是否对此有一个解释:

I wonder if anybody has ever stumbled upon this one and if there is an explanation for this:

我有一个小型测试项目:

I have a small test project:

<Window x:Class="Test.MainWindow"
       
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       
Title="Text Project">

   
<StackPanel>

       
<ListView Name="ListView"
                 
Panel.ZIndex="2"
                 
KeyboardNavigation.ControlTabNavigation="Contained"
                 
KeyboardNavigation.TabNavigation="Contained"
                 
KeyboardNavigation.DirectionalNavigation="Contained">

           
<ListView.View>
               
<GridView>
                   
<GridViewColumn DisplayMemberBinding="{Binding}" />
               
</GridView>
           
</ListView.View>

       
</ListView>

       
<TextBox Text="foo" Panel.ZIndex="1"/>

   
</StackPanel>

</Window>

及其背后的代码:

namespace Test
{
   
public partial class MainWindow : Window
   
{
       
public MainWindow()
       
{
           
InitializeComponent();

           
var itemsSource = new ObservableCollection<string>();
           
for (int i = 0; i < 10; i++) itemsSource.Add("item n° " + i);

           
ListView.ItemsSource = itemsSource;
       
}
   
}
}

基本上,有一个ListView以gridView作为视图,还有一个文本框.

basically, there is a ListView with a gridView as a View, and a textbox.

我的目标是在列表框上包含TabNavigation = Contain,以便在选择最后一项"并按Tab时,焦点不会移至文本框,而是停留在ListView的最后一项上.

My goal is to have the TabNavigation=Contained on the listBox so that when the Last item is selected and I press tab, the focus does not go to the textBox but stays on the ListView's last item.

-

现在这是棘手的地方:

您将看到我已经在两个元素上设置了Canvas.ZIndex属性:Listview(= 2)和TextBox(= 1).和 用一种我能弄清楚但无法解释的方式来纠正tabNavigation的行为:

You'll see that I have set the Canvas.ZIndex property on both elements: the Listview (=2) and the TextBox (=1). And this messes up the behavior of the tabNavigation in a way I could figure out but not explain:

  • 如果ListView的Canvas.Zindex优于或等于TextBox的Canvas.ZIndex,则一切按预期工作:焦点停留在ListView的最后一项上.

  • If the ListView's Canvas.Zindex is superior or equal to the TextBox's Canvas.ZIndex, everything works as expected: the focus stays on the ListView's last item.

但是如果ListView的Canvas.ZIndex严格低于TextBox的Canvas.ZIndex,则焦点将移到textBox上.您可以在家里尝试通过将TextBox的Canvas.ZIndex属性设置为1、2和3(使用ListView的Canvas.ZIndex)来进行尝试. 剩余的= 1)

but If the ListView'sCanvas.ZIndex is strictly inferior to the TextBox's Canvas.ZIndex, then the focus goes to the textBox. You can try this at home by setting the TextBox's Canvas.ZIndex property alternatively to 1, 2 and 3 (with the ListView's Canvas.ZIndex remaining =1)

有人可以解释吗?

在我的情况下,我真的需要ListView在我的应用程序中的所有其他元素之上都具有一个ZIndex.尽管有这种行为,有没有办法实现我所要实现的目标,而不必自己管理Tab键?

In my case, I really need the ListView to have a ZIndex above everything else in my App. Is there a way to achieve what I what in spite of this behavior, without having to manage the Tab key Myself?

编辑:奇怪的是,我在所有情况下都能正常使用Keyboard.DirectionalNavigation属性,但没有任何问题.只是TabNavigation和ControlTabNavigation使我头疼

strangely enough, I do not have any issue with the Keyboard.DirectionalNavigation property wich behaves correctly in all cases. It's just the TabNavigation and the ControlTabNavigation that are giving me a headache

推荐答案

嗨Baltyr,

Hi Baltyr,

我还没有找到问题的根本原因,但是,根据您的描述,我认为您可以将IsTabStop属性设置为TextBox来实现您的目标.

I have not found the root cause of your issue, however, based on your description, I think you could set IsTabStop property to your TextBox to achieve your goal.

例如,当ListView成为keyBoard焦点时,可以从选项卡导航范围中删除TextBox,然后可以正常设置ListView的zIndex.

For example, when your ListView got the keyBoard focus, you could remove TextBox from tab navigation scope, then you could set ListView's zIndex as normal.

<TextBox Text="foo" IsTabStop="False" Panel.ZIndex="1"/>

最好的问候


这篇关于KeyBoard.TabNavigation =“包含" + Canvas.ZIndex ="2"在WPF中的listView上给出奇怪的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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