2010-08-24 76 views
11

我想從列表框中選擇它時,要更改ListBoxItem上的控件模板。爲此,我將從ListBox本身獲取選定的ListBoxItem,並在其上設置控件模板。我將如何去做這件事?我試過了,SelectedItem並返回ListBoxItem中的綁定對象。獲取列表框中的ListBoxItem

回答

20

您可以通過使用ItemContainerGenerator檢索綁定項的項容器:

object selectedItem = listBox.SelectedItem; 
ListBoxItem selectedListBoxItem = listBox.ItemContainerGenerator.ContainerFromItem(selectedItem) as ListBoxItem; 
0

現在你可以用這個做到這一點:

ListBoxItem container = listBox.ContainerFromIndex(listBox.SelectedIndex) as ListBoxItem; 

ItemContainerGenerator.ContainerFromItem()功能現在好像obsolete

如果你已經設置的項目模板列表框,然後你可以從

UIElement item= container.ContentTemplateRoot; 
得到它
相關問題