想在Listbox的SelectionChanged事件触发时对之前的选项进行处理。但是listbox没有previewSelectionChanged事件。
解决办法:
1. Validation
因为要处理的项在TextBox中,所以可以给TextBox添加一个Validation。但是由于TextBox是与Property Binding,而Validation是在binding赋值之前进行操作(调试过程中是这样的流程),所以无法获取textbox中的值。如果没有binding应该是可以。
参考:https://social.msdn.microsoft.com/Forums/vstudio/en-US/e96c725e-a86d-427e-944b-fcc4273ac260/any-way-to-preview-the-listviewselectionchanged-event?forum=wpf
2. 获取SelectionChangedEventArgs中的RemovedItem。最后采用的这个方法。
private void ListBox_SelectionChanged(object sender , SelectionChangedEventArgs e)
{
// Here are your old selected items from the selection changed.
// If your list box does not allow multiple selection, then just use the index 0
// but making sure that the e.RemovedItems.Count is > 0 if you are planning to address by index.
IList oldItems = e.RemovedItems;
// Do something here.
// Here are you newly selected items.
IList newItems = e.AddedItems;
}
参考:http://stackoverflow.com/questions/1548237/how-to-get-something-like-previewselectionchanged-event-in-listbox
时间: 2024-09-30 06:41:22