[英]Get the ListBoxItem in a ListBox
I am trying to change the Control template on a ListBoxItem when It is selected from the ListBox. 我正在尝试从ListBox中选择它时更改ListBoxItem上的Control模板。 To do so, I was going to get the selected ListBoxItem from the ListBox itself, and set the control template on that.
为此,我将从ListBox本身获取选定的ListBoxItem,并在其上设置控件模板。 How would i go about doing this?
我该怎么做呢? I have tried, SelectedItem and that returns the bound object within the ListBoxItem.
我试过,SelectedItem并返回ListBoxItem中的绑定对象。
You can retrieve the item container from the bound item by using the ItemContainerGenerator
: 您可以使用
ItemContainerGenerator
从绑定项中检索项容器:
object selectedItem = listBox.SelectedItem;
ListBoxItem selectedListBoxItem = listBox.ItemContainerGenerator.ContainerFromItem(selectedItem) as ListBoxItem;
Now you can do it with this: 现在你可以这样做:
ListBoxItem container = listBox.ContainerFromIndex(listBox.SelectedIndex) as ListBoxItem;
The ItemContainerGenerator.ContainerFromItem()
function seems like obsolete now. ItemContainerGenerator.ContainerFromItem()
函数现在看起来已经过时了。
If you have set the Item Template for the ListBox then you can get it from 如果您已为ListBox设置了项目模板,那么您可以从中获取它
UIElement item= container.ContentTemplateRoot;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.