<p>Is there a way to get at the ItemContaner of a selected item in a listbox?  In Silverlight 2.0 Beta 1 I could, but the container is hidden in Beta 2 of Silverlight 2.0. </p>

<p>I'm trying to resize the listbox item when it is unselected to a specific size and when selected to a variable size.  I also want to get the relative position of the selected item for animations.   Growing to a variable size and getting the relative pasition is why i need to get to the listbox item.</p>

<p>I should clarify i'm not adding items to the listbox explicitly.  I am using data binding in xaml and DataTemplates.  What I have trouble accessing is the ItemContainer of the selected item's DataTemplate.</p>

[{'answer_id': 86980, 'author': 'dcstraw', 'author_id': 10391, 'author_profile': 'https://Stackoverflow.com/users/10391', 'pm_score': 0, 'selected': False, 'text': "<p>If you are adding non-UI elements to the listbox (such as strings or non-UI data objects), then this is probably pretty difficult.  However if you wrap your items in some sort of FrameworkElement-derived object before adding them to the listbox, you can use TransformToVisual to get the relative size and use Height and Width to set the size of the item.</p>\n\n<p>In general you can wrap your objects in a ContentControl like the following.  Instead of:</p>\n\n<pre><code>_ListBox.Items.Add(obj0);\n_ListBox.Items.Add(obj1);\n</code></pre>\n\n<p>Do this:</p>\n\n<pre><code>_ListBox.Items.Add(new ContentControl { Content = obj0 });\n_ListBox.Items.Add(new ContentControl { Content = obj1 });\n</code></pre>\n\n<p>Now when you get _ListBox.SelectedItem you can cast it to ContentControl and set the size and get the relative position.  If you need the original object, simply get the value of the item's Content property.</p>\n"}, {'answer_id': 162051, 'author': 'MaxM', 'author_id': 4226, 'author_profile': 'https://Stackoverflow.com/users/4226', 'pm_score': 2, 'selected': False, 'text': "<p>There is a way to obtain the Panel containing the item's UIElement and the mapping of items to UIElements. You have to inherit from ListBox (this actually works for any ItemsControl) and override PrepareContainerForItemOverride:</p>\n\n<pre><code>protected override void PrepareContainerForItemOverride(DependencyObject element, object item)\n    {\n        base.PrepareContainerForItemOverride(element, item);\n        var el = element as FrameworkElement;\n        if (el != null)\n        {\n            // here is the elements's panel:\n            _itemsHost = el.Parent as Panel;\n\n            // item is original item inserted in Items or ItemsSource\n            // we can save the mapping between items and FrameworElements:\n            _elementMapping[item] = el;\n        }\n    }\n</code></pre>\n\n<p>This is kind of hackish, but it works just fine.</p>\n"}, {'answer_id': 1500946, 'author': 'jpierson', 'author_id': 83658, 'author_profile': 'https://Stackoverflow.com/users/83658', 'pm_score': 0, 'selected': False, 'text': '<p>It appears that you can use relative binding to get at the Item Container from the ItemTemplate.</p>\n\n<pre><code>&lt;TextBlock YourTargetProperty="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListBoxItem}}, Mode=OneWay, Path=YourSourceProperty}" /&gt;\n</code></pre>\n\n<p>I found this solution <a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fbe80312-67aa-42ca-bd99-89b6d79e06ae" rel="nofollow noreferrer">here</a>.</p>\n'}, {'answer_id': 10407613, 'author': 'Parrhesia Joe', 'author_id': 542909, 'author_profile': 'https://Stackoverflow.com/users/542909', 'pm_score': 0, 'selected': False, 'text': '<p>Update for silverlight 5.</p>\n\n<pre><code>              &lt;ListBox ItemsSource="{Binding Properties}"&gt;\n                 &lt;ListBox.ItemTemplate&gt;\n                    &lt;DataTemplate&gt;\n                       &lt;TextBlock Text="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem}}" /&gt;\n                    &lt;/DataTemplate&gt;\n                 &lt;/ListBox.ItemTemplate&gt;\n</code></pre>\n\n<p>RelativeSource AncestorType is now supported, making this much easier.</p>\n'}]
['https://Stackoverflow.com/questions/59653', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/580/']