WPF Tips: Listbox SelectionChanged觸發前的選項


想在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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM