开发者

Get the ListBoxItem in a ListBox

开发者 https://www.devze.com 2023-01-12 16:58 出处:网络
I am trying to change the Control template on a ListBoxItem when It is selected from the ListBox. To do so, I was going to get the selected ListBoxItem from the ListBox itself, and set the control tem

I am trying to change the Control template on a ListBoxItem when It is selected from the ListBox. To do so, I was going to get the selected ListBoxItem from the ListBox itself, and set the control temp开发者_JAVA技巧late on that. How would i go about doing this? I have tried, SelectedItem and that returns the bound object within the ListBoxItem.


You can retrieve the item container from the bound item by using the 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.

If you have set the Item Template for the ListBox then you can get it from

UIElement item= container.ContentTemplateRoot;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号