2010-10-19 24 views
0

我已經使用ff掛鉤了ListBoxItems的雙擊事件。代碼我的XAML:雙擊選中所有的ListBoxItems

<Style TargetType="{x:Type ListBoxItem}"> 
     <EventSetter Event="MouseDoubleClick" Handler="onMouseDoubleClickOnListBoxItem" /> 
    </Style> 

該處理程序的代碼是:

private void onMouseDoubleClickOnListBoxItem(object sender, MouseButtonEventArgs e) 
    { 
     Debug.Print("Going to select all."); 
     listBox.SelectAll(); 
     Debug.Print("Selected all."); 
    } 

當我運行它,我看到了調試輸出,但內容並非全都在屏幕上得到選擇。

回答

1

試用SelectionMode作爲倍數。

更新,

在擴展模式上進行雙擊將其重置爲的SelectedItem的項目,這是因爲在同一線程上執行選擇單個項目的單擊事件採取行動。

爲了達到這個目的,我在雙擊事件處理函數上調用了一個委託方法(在類範圍中),並從那裏調用SelectAll調用主窗口上的列表框Dispatcher 。

一樣,

// delegate 
delegate void ChangeViewStateDelegate(); 

// on double click event invoke the custom method 
private void onMouseDoubleClickOnListBoxItem (object sender, MouseButtonEventArgs e) { 
    ChangeViewStateDelegate handler = new ChangeViewStateDelegate (Update); 
    handler.BeginInvoke (null, null); 
} 

// in the custom method invoke the selectall function on the main window (UI which created the listbox) thread 
private void Update() { 
    ChangeViewStateDelegate handler = new ChangeViewStateDelegate (UIUpdate); 
    this.Dispatcher.BeginInvoke (handler, null); 
} 

// call listbox.SelectAll 
private void UIUpdate() { 
    lstBox.SelectAll(); 
} 
+0

selectionMode是已經在延長。 – 2010-10-19 03:38:32

+0

是的,但我認爲在擴展模式下執行雙擊的項目會重置爲selecteditem。我不確定這個沒有設置來驗證。 – whoisthis 2010-10-19 03:50:08

+0

對不起,你是對的。多選作爲選擇模式產生更好的行爲。但仍有一項未選中:我雙擊的項目。另外,我需要使用擴展選擇模式。如果你用兩者的解決方案來編輯​​你的答案,我可以撤消投票並接受你的答案。 – 2010-10-19 05:12:03