2012-03-23 34 views
0

我在WPF中有一個分層樹狀圖。我使用http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/作爲視圖模型的事件綁定。TreeView中最高的祖先作爲命令參數

當我雙擊treeview中的一個項目時,我總是想要傳遞根類型作爲commandparameter。

實施例:

..B
....Ç
d
..E

當我點擊 'A', 'B' 或 'C' 我想'A'作爲命令參數。 當我點擊'D'或'E'時,我想'D'作爲命令參數。

這是我到目前爲止,這始終把項目作爲點擊commandparameter:

<Setter Property="acb:CommandBehavior.Event" Value="MouseDoubleClick"/> 
    <Setter Property="acb:CommandBehavior.Command" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.SomeCommand}"/> 
    <Setter Property="acb:CommandBehavior.CommandParameter" Value="{Binding}"/> 

我想我必須做的RelativeSource綁定,我不出來..

問候,

米歇爾

+0

如果您知道每個項目嵌套的級別,則可以使用「Binding RelativeSource = {RelativeSource Mode = FindAncestor,AncestorType = {x:Type TreeViewItem},AncestorLevel = 2},Path = DataContext.AProperty}」您需要正確設置AncestorLevel。 – Phil 2012-03-23 11:23:02

回答

1

據我所知,你不能創建一個綁定,相當於想要的表達,你可能會更好傳遞CUR租用項目作爲參數並在命令代碼中找到根節點。