问题描述
我目前正在第一次尝试C#和WPF.
I'm currently trying out C# and WPF for the first time.
我有一个带有两个选项卡tabA
和tabB
的窗口.每个选项卡都有一个列表框(它们几乎相同),tabA
具有listboxA
,tabB
具有listboxB
.当选择了列表框的项,关于该项目的一些信息被示出,这样我知道它被选中.
I have a window with two tabs, tabA
and tabB
. Each tab has a list box (they are almost identical), tabA
has listboxA
and tabB
has listboxB
. When an item in a list box is selected, some information about the item is shown, so that I know that it is selected.
在特定事件E
中,我希望突出显示并选择listboxA
或listboxB
中的一项,该项由我的应用程序中的逻辑确定.
On a certain event E
, I want an item in listboxA
or listboxB
to be highlighted and selected, which one is determined by the logic in my application.
方案A是我当前在tabA
上.出现E
时,将选择listboxA
中的项目.我先呼叫listboxA.Focus()
,然后再呼叫listboxA.SelectedItem = item
.这很好.选择item
,将显示有关该项目的信息,并在列表中突出显示该项目.
Scenario A is that I am currently on tabA
. When E
occurs, an item in listboxA
is chosen. I call listboxA.Focus()
, and then listboxA.SelectedItem = item
. This works fine. item
is selected, so the information about the item is shown, and the item is highlighted in the list.
我遇到问题的情况B是我当前在tabA
上.出现E
时,将选择listboxB
中的项目.我调用tabControl.SelectedItem = tabB
来切换选项卡,效果很好.与方案A中一样,我先调用listboxB.Focus()
,然后再调用listboxB.SelectedItem = item
.之所以选择该项目,是因为显示了有关该项目的信息,但是事情是这样的:该项目未在列表框中突出显示.
Scenario B, where I have my problem, is that I am currently on tabA
. When E
occurs, an item in listboxB
is chosen. I call tabControl.SelectedItem = tabB
to switch the tab, which works fine. As in Scenario A, I call listboxB.Focus()
and then listboxB.SelectedItem = item
. The item is selected, because the information about the item is shown, but, here's the thing: The item is not highlighted in the list box.
从tabB
更改为tabA
时也会发生这种情况.更改选项卡时总是会发生这种情况,但是如果我留在同一选项卡中,突出显示效果会很好.
This also happens when changing from tabB
to tabA
. It always happens when I change the tab, but if I stay in the same tab the highlight works fine.
除了调用Focus()
之外,还有人知道我是否还需要做其他操作来使列表框成为焦点?
Does anyone know if there's anything else I need to do to give the list box the focus after changing the tab, apart from calling Focus()
?
推荐答案
WPF具有焦点作用域,列表框可能会聚焦在其作用域内部,但如果作用域本身未聚焦,则可能是作用域本身.另外,列表框可以具有焦点,但是它包含需要重点关注的项目的单独范围.
WPF has focus scopes, the list box may be focused inside its scope but the scope itself if may not be focused. Alternatively the list box could have focus but it contains a separate scope for its items which would need to be focused.
这篇关于列表框不突出显示选项卡更改时的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!