Author Topic: Iterate all items in WPF ListBox - ItemContainerGenerator.ContainerFromItem  (Read 1474 times)

administrator

  • Administrator
  • Full Member
  • *****
  • Posts: 69
If you add items at run time and if you want add events for each list box item, then you may have to go through all the items in the list box. You can use  the ItemContainerGenerator.ContainerFromItem to go through all the items in the list box.


<Button Name="Event" Content="Event" Click="AddEvent_Click" Width="40" Height="24" Margin="10,0,0,0" HorizontalAlignment="Center"  VerticalAlignment="Center"></Button>

<ListBox Grid.Row="2" Name="gui_studentListBox"  Width="240" Height="176" Margin="15,22,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Visibility="Visible" SelectionMode="Single" ItemsSource="{Binding}"/>


private void AddEvent_Click(object sender, RoutedEventArgs e)
{
    for (int i = 0; i < gui_studentListBox.Items.Count; i++)
    {
ListBoxItem lbi = (ListBoxItem) gui_studentListBox.ItemContainerGenerator.ContainerFromItem(gui_studentListBox.Items);
      lbi.MouseDoubleClick += new MouseButtonEventHandler(lbi_MouseDoubleClick);
     }
}

void lbi_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
     throw new NotImplementedException();
}

« Last Edit: October 24, 2011, 12:54:30 pm by admin softwareandfinance »

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1