2010-06-01 43 views
0

如果我在TreeViewItem上處理PreviewMouseDown事件,那麼我會點擊TreeViewItem的左側(Windows XP)包含的小+/-框來獲取事件。我如何區分這一點?TreeViewItem上的WPF TreeView PreviewMouseDown

我試過如下:

// We've had a single click on a tree view item 
// Unfortunately this is the WHOLE tree item, including the +/- 
// symbol to the left. The tree doesn't do a selection, so we 
// have to filter this out... 
MouseDevice device = e.Device as MouseDevice; 

// This is bad. The whole point of WPF is for the code 
// not to know what the UI has - yet here we are testing for 
// it as a workaround. Sigh... 

// This is broken - if you click on the +/- on a item, it shouldn't 
// go on to be processed by OnTreeSingleClick... ho hum! 
if (device.DirectlyOver.GetType() != typeof(Path)) 
{ 
    OnTreeSingleClick(sender); 
} 

我後是不包括它似乎包括額外的比特樹型視圖項目只是單一的點擊。

回答

0

我的解決方案是使用SelectionItemChanged事件,似乎避免了+/-按鈕的問題。

0

如果我的理解是正確的,e.OriginalSource應該包含您控制的實際上點擊。確保它是一個TextBlock(或其他任何實際在你的TreeView中)。如果您是偏執狂的,您也可以使用類似this post的東西來確定OriginalSource屬於哪個TreeViewItem。

+0

我在找的是一種檢測是否點擊了TreeViewItem左邊的+/-按鈕的方法。我所看到的是邊界或路徑。不幸的是,如果你點擊文本塊外的文本塊,你會得到一個邊界。所以我無法區分點擊TreeViewItem或+/-。我看到的唯一的獨特之處是Path - 但是,如果我在TreeViewItem中使用Path項,我該如何區分它們? – imekon 2010-06-02 08:42:56

+0

也許設置任何你放在TreeViewItem的「內容」的東西的標籤屬性,以區別於其他東西? – JustABill 2010-06-02 15:09:51