触发的SelectedIndex改变,同时单击任意控制ListBoxItem的区域内 [英] Trigger SelectedIndex changed whilst clicking on any control within a ListBoxItem area

查看:654
本文介绍了触发的SelectedIndex改变,同时单击任意控制ListBoxItem的区域内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为ListBoxItem的数据模板,其中包含的几个按钮,而像网格或图表一些自定义的控件。每个按钮绑定到相应的命令处理程序,ListView控件的SelectedIndex属性必将视图模型的属性格式为好。

I've a data template for ListBoxItem which contains of few buttons, and few custom controls like Grid or Chart. Each button is bound to an appropriate command handler, SelectedIndex property of a ListView control is bound to ViewModel's propery as well.

问题:我无法解析当前选择的项目/指标,因为它并没有改变,而点击一个按钮或其他控制一个ListBox中的命令处理程序(这是必然的按钮)项目,但是当我点击一个ListBoxItem地区本身 - 的SelectedIndex改变

The problem: in command handlers (which are bound to buttons) I can't resolve currently selected item/index because it is not changing whilst clicking on a button or an other control within a ListBox item, but when I clicking on ListBoxItem area itself - SelectedIndex is changing.

的问题是如何触发的SelectedIndex被同时点击任何控制范围内的ListBoxItem的变化?

Question is how to trigger SelectedIndex to be changed whilst clicking on any control within ListBoxItem?

推荐答案

添加到您的 ListBox.Resources

<ListBox.Resources>
    <Style TargetType="{x:Type ListBoxItem}">
        <Style.Triggers>
            <Trigger Property="IsKeyboardFocusWithin" Value="True">
                <Setter Property="IsSelected" Value="True" />
            </Trigger>
        </Style.Triggers>
    </Style>
</ListBox.Resources>

修改

在previous方法不仅使选择,只要它具有键盘焦点的ListBoxItem中。如果将集中出ListBoxItem中,就再次成为未选中。

The previous method only makes the ListBoxItem selected for as long as it has keyboard focus. If you move focus out of the ListBoxItem, it becomes unselected again.

下面是另外一个简单的方法来选择ListBox中的项目时,它保持选定的键盘焦点的项目内移动,而当焦点移出ListBoxItem中的

Here's another simple way to select a ListBox item when the keyboard focus moves within the item, and it stays selected when focus is moved out of the ListBoxItem

<Style TargetType="{x:Type ListBoxItem}">
    <EventSetter Event="PreviewGotKeyboardFocus" Handler="SelectCurrentItem"/>
</Style>

而在code

And in the Code Behind

protected void SelectCurrentItem(object sender, KeyboardFocusChangedEventArgs e)
{
    ListBoxItem item = (ListBoxItem)sender;
    item.IsSelected = true;
}

这篇关于触发的SelectedIndex改变,同时单击任意控制ListBoxItem的区域内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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