I have a ListBox
where I defined a DataTemplate
for its items:
<ListBox.ItemTemplate>
<DataTemplate>
<Border>
<TextBlock Text="{Binding Name}" />
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
And the class I use to add elements into the ListBox
is the following:
public class MyItem
{
public string Name
{ get; set; }
}
Now I need to change the background of the ListBox
item, when for example the item has been selected:
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count == 1)
{
var item = e.AddedItems.First() as MyItem;
}
}
the problem is that the item is of type MyItem
, while I need access to the Border
and TextBlock
objects as well.