简体   繁体   English

WPF:在代码中设置ListBoxItem选择事件

[英]WPF: set ListBoxItem select event in code

Okey, in code (C#) I create some ListBoxItems and add them to a UniformGird that gets put in a Viewbox defined in XAML (and for practical reasons I can't define a ListBox on XAML). Okey,在代码(C#)中,我创建了一些ListBoxItem并将它们添加到UniformGird中,该UniformGird放入XAML中定义的Viewbox中(出于实际原因,我无法在XAML上定义ListBox)。

When a ListBoxItem is Selected I need to fire an event that calls a method I have made. 当一个ListBoxItem被选中时,我需要触发一个事件,该事件调用我已经建立的方法。

In XAML I would just set an EventSetter that calls the method, but I'm not sure how I can do the same thing in C# code for ListBoxItem. 在XAML中,我只是设置一个EventSetter来调用该方法,但是我不确定如何在C#代码中为ListBoxItem做同样的事情。

Hook up an event handler for the Selected or PreviewMouseLeftButtonDown event: SelectedPreviewMouseLeftButtonDown事件连接一个事件处理程序:

ListBoxItem lbi = new ListBoxItem();
lbi.PreviewMouseLeftButtonDown += (se, ee) => { MessageBox.Show("do something..."); };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在RightTapped事件下选择ListBoxItem - Select ListBoxItem under RightTapped event WPF单击ListBoxItem内的控件不选择ListBoxItem - WPF Click on control inside ListBoxItem does not select ListBoxItem WPF 和 Prism - 绑定到 TemplateSelector 驱动的 ListBoxItem 的 IsSelected 事件未在 ViewModel 绑定代码中触发 - WPF and Prism - Binding to TemplateSelector driven ListBoxItem's IsSelected event is not firing in ViewModel bound code WPF C#ListBoxItem代码背后的if语句 - WPF C# ListBoxItem Code Behind for If Statement WPF ComboBox按代码阻止选择项目上的事件 - WPF ComboBox prevent event on select item by code 在 ListBoxItem DataTemplate 内的嵌套控件中处理相同的单击事件 - WPF - Handling the same click event in a nested control inside ListBoxItem DataTemplate - WPF 在WPF中的ListBoxItem上触发触发器时,如何设置父元素的属性 - How to set a property of the parent element, when firing a trigger on ListBoxItem in WPF WPF ListBoxItem(s)在滚动后丢失以编程方式设置的样式 - WPF ListBoxItem(s) losing programatically set styles after scroll 从 WPF 中的代码后面更改 ListBoxItem 的前景 - Changing ListBoxItem's Foreground from Code Behind in WPF 获取和 Select 从 ListBox_PreviewMouseLeftButtonUp 事件单击的 ListBoxItem - Get and Select the ListBoxItem clicked from ListBox_PreviewMouseLeftButtonUp event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM