I Suppose you wanted this, your question is not very clear.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
CreateStackPanel();
}
private void CreateStackPanel()
{
StackPanel MyStackPanel = new StackPanel();
MyStackPanel.Orientation = Orientation.Horizontal;
ListBox lstBox = new ListBox();
lstBox.Items.Add("item1");
lstBox.Items.Add("item2");
lstBox.Items.Add("item3");
lstBox.Items.Add("item4");
lstBox.Items.Add("item5");
lstBox.SelectionChanged += new SelectionChangedEventHandler(OnSelectionChanged);
MyStackPanel.Children.Add(lstBox);
RootWindow.Content = MyStackPanel;
}
void OnSelectionChanged(object sender, RoutedEventArgs e)
{
MessageBox.Show("You have selected " + ((System.Windows.Controls.ListBox)(sender)).SelectedItems[0].ToString() + ".");
}