[英]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: 为
Selected
或PreviewMouseLeftButtonDown
事件连接一个事件处理程序:
ListBoxItem lbi = new ListBoxItem();
lbi.PreviewMouseLeftButtonDown += (se, ee) => { MessageBox.Show("do something..."); };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.