需要有关ListBox事件处理程序的帮助. [英] Need help with a ListBox event handler.

查看:66
本文介绍了需要有关ListBox事件处理程序的帮助.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用以下代码来在单击数据模板中的控件时将listboxitem设置为选中状态.

I am currently using the following code to set the listboxitem as selected when a control in the datatemplate is clicked.

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

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

使用此XAML要求后面提供以下代码:

using this XAML requires the following code behind:

受保护的void SelectCurrentItem(对象发送者,KeyboardFocusChangedEventArgs e)
{
  ListBoxItem item =(ListBoxItem)sender;
  item.IsSelected = true;
}

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

后面的代码要求发件人对象标识要选择的listboxitem.

The codebehind requires the sender object to identify the listboxitem to be selected.

是否可以将处理程序绑定到基础ViewModel并获得相同的结果?

Is there a way to bind the handler to the underlying ViewModel and achieve the same result?

谢谢

Robotuner

Robotuner

推荐答案

如果我正确理解了这个问题,您可以改为使用触发器".将此放置在Window/Page/ListBox资源中.

If I understand the question correctly, you could use a Trigger instead. Place this in the Window/Page/ListBox resources.

  

  

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

 

此致,
皮特

 

Regards,
Pete


这篇关于需要有关ListBox事件处理程序的帮助.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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