I am trying to program a ListBoxItem
Selected
event in code because I need dynamic ListBoxItems
. I am coding this in wpf, the following xaml works great:
<ListBoxItem Tag="cPage_Mod_Modules" Selected="ListBoxItem_Selected">
<StackPanel Orientation="Horizontal">
<TextBlock Style="{StaticResource sColor01}" Text="» " />
<TextBlock Text="Moduler" VerticalAlignment="Center" Focusable="True" />
</StackPanel>
</ListBoxItem>
The Selected="ListBoxItem_Selected"
works fine.
But when I try to create the ListBoxItem
in code, it doesn't work. Here is my code:
IList<ListBoxItem> lbi = new List<ListBoxItem>();
ListBoxItem itemBox = new ListBoxItem();
itemBox.Tag = "cPage_Assignment_Overview";
itemBox.Selected += new EventHandler(ListBoxItem_Selected(this, null));
lbTask.Items.Add(itemBox);
I just want to route to the event ListBoxItem_Selected(object sender, RoutedEventArgs e)
when somebody is selecting an item.
Do you mean how to wire up the event ? This should do it (assuming the function signature is compatible with the event handler signature ).
itemBox.Selected += ListBoxItem_Selected;