title: How add the event of mouse over in a ListBox on Code Behind
body: <pre><code>private ListBox linkList = new ListBox();
  </code></pre>
  
  <p>here I want to add the event of mouseOver for each item from the linkList</p>
  
  <p>I tried: </p>
  
  <pre><code>foreach(ListItem item in linkList.Items){
      ...
  }
  </code></pre>
  
  <p>but didn't work
  what can I do?</p>
  
  <p>this is how the ListBox is filling :</p>
  
  <pre><code>&lt;ListBox x:Name="LinkList" ItemsSource="{TemplateBinding Links}"  
              ScrollViewer.HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" &gt;
      &lt;ListBox.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;Grid Height="50" Background="Transparent" Width="500"&gt;
                  &lt;Border Padding="10"&gt;
                      &lt;Path x:Name="icon" Data="{Binding IconData}" Stretch="Fill" Fill="{DynamicResource Accent}" Width="20" Height="20" HorizontalAlignment="Left" VerticalAlignment="Center" /&gt;
                  &lt;/Border&gt;
                  &lt;TextBlock x:Name="texto" ToolTip="{Binding Tooltip}"  Text="{Binding DisplayName}" Margin="45,2,2,2" FontSize="{DynamicResource MediumFontSize}" TextTrimming="CharacterEllipsis" VerticalAlignment="Center" HorizontalAlignment="Left" /&gt;
              &lt;/Grid&gt;
              &lt;DataTemplate.Triggers&gt;
                  &lt;DataTrigger Binding="{Binding IconData}" Value="{x:Null}"&gt;
                      &lt;Setter Property="Margin" TargetName="texto"&gt;
                          &lt;Setter.Value&gt;
                              &lt;Thickness Bottom="2" Top="2" Left="10" Right="2"/&gt; 
                          &lt;/Setter.Value&gt;
                      &lt;/Setter&gt;
                  &lt;/DataTrigger&gt;
                  &lt;Trigger Property="IsMouseOver" Value="true"&gt;
                      &lt;Setter Property="Fill" TargetName="icon"&gt;
                          &lt;Setter.Value&gt;
                              &lt;SolidColorBrush Color="#f2f2f2" /&gt;
                          &lt;/Setter.Value&gt;
                      &lt;/Setter&gt;
                  &lt;/Trigger&gt;
  
              &lt;/DataTemplate.Triggers&gt;
          &lt;/DataTemplate&gt;
      &lt;/ListBox.ItemTemplate&gt;
  &lt;/ListBox&gt;
  </code></pre>
  
  <p>Update:</p>
  
  <p>Code Behind:</p>
  
  <pre><code>public partial class ModernVerticalMenu
      : Control
  {
      /// &lt;summary&gt;
      /// Identifies the ContentLoader dependency property.
      /// &lt;/summary&gt;
      public static readonly DependencyProperty ContentLoaderProperty = DependencyProperty.Register("ContentLoader", typeof(IContentLoader), typeof(ModernVerticalMenu), new PropertyMetadata(new DefaultContentLoader()));
      /// &lt;summary&gt;
      /// Identifies the Layout dependency property.
      /// &lt;/summary&gt;
      public static readonly DependencyProperty LayoutProperty = DependencyProperty.Register("Layout", typeof(TabLayout), typeof(ModernVerticalMenu), new PropertyMetadata(TabLayout.Tab));
      /// &lt;summary&gt;
      /// Identifies the ListWidth dependency property.
      /// &lt;/summary&gt;
      public static readonly DependencyProperty ListWidthProperty = DependencyProperty.Register("ListWidth", typeof(GridLength), typeof(ModernVerticalMenu), new PropertyMetadata(new GridLength(170)));
      /// &lt;summary&gt;
      /// Identifies the Links dependency property.
      /// &lt;/summary&gt;
      public static readonly DependencyProperty LinksProperty = DependencyProperty.Register("Links", typeof(LinkCollection), typeof(ModernVerticalMenu), new PropertyMetadata(OnLinksChanged));
      /// &lt;summary&gt;
      /// Identifies the SelectedSource dependency property.
      /// &lt;/summary&gt;
      public static readonly DependencyProperty SelectedSourceProperty = DependencyProperty.Register("SelectedSource", typeof(Uri), typeof(ModernVerticalMenu), new PropertyMetadata(OnSelectedSourceChanged));
      /// &lt;summary&gt;
      /// Defines the SelectedLinkGroup dependency property.
      /// &lt;/summary&gt;
      public static readonly DependencyProperty SelectedLinkGroupProperty = DependencyProperty.Register("SelectedLinkGroup", typeof(LinkCollection), typeof(ModernVerticalMenu), new PropertyMetadata(OnSelectedLinkGroupChanged));
      /// &lt;summary&gt;
      /// Defines the SelectedLink dependency property.
      /// &lt;/summary&gt;
      public static readonly DependencyProperty SelectedLinkProperty = DependencyProperty.Register("SelectedLink", typeof(Link), typeof(ModernVerticalMenu), new PropertyMetadata(OnSelectedLinkChanged));
  
  
      /// &lt;summary&gt;
      /// Occurs when the selected source has changed.
      /// &lt;/summary&gt;
      public event EventHandler&lt;SourceEventArgs&gt; SelectedSourceChanged;
  
      private ListBox linkList;
  
      /// &lt;summary&gt;
      /// Initializes a new instance of the &lt;see cref="ModernVerticalMenu"/&gt; control.
      /// &lt;/summary&gt;
      public ModernVerticalMenu()
      {
          this.DefaultStyleKey = typeof(ModernVerticalMenu);
  
          // create a default links collection
          SetCurrentValue(LinksProperty, new LinkCollection());
      }
  
      private static void OnSelectedLinkGroupChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
      {
          // retrieve the selected link from the group
          var group = (Link)e.NewValue; // cria uma nova instancia do grupo
          Link selectedLink = null; //cria um link selecionado
          if (group != null)
          { //se o grupo copiado existe
              selectedLink = group; //
  
              // if no link selected or link doesn't exist in group links, auto-select first
              if (group.Children != null)
              {
                  if (selectedLink != null &amp;&amp; !group.Children.Any(l =&gt; l == selectedLink))
                  {
                      selectedLink = null;
                  }
  
                  if (selectedLink == null)
                  {
                      selectedLink = group.Children.FirstOrDefault();
                  }
              }
          }
  
          // update the selected link
          ((ModernVerticalMenu)o).SetCurrentValue(SelectedLinkProperty, selectedLink);
      }
  
      private static void OnSelectedLinkChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
      {
          // update selected source
          var newValue = (Link)e.NewValue;
          Uri selectedSource = null;
          if (newValue != null)
          {
              selectedSource = newValue.Source;
          }
          ((ModernVerticalMenu)o).SetCurrentValue(SelectedSourceProperty, selectedSource);
      }
  
  
      private static void OnLinksChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
      {
          ((ModernVerticalMenu)o).UpdateSelection();
      }
  
      private static void OnSelectedSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
      {
          ((ModernVerticalMenu)o).OnSelectedSourceChanged((Uri)e.OldValue, (Uri)e.NewValue);
      }
  
      private void OnSelectedSourceChanged(Uri oldValue, Uri newValue)
      {
          UpdateSelection();
  
          // raise SelectedSourceChanged event
          var handler = this.SelectedSourceChanged;
          if (handler != null) {
              handler(this, new SourceEventArgs(newValue));
          }
      }
  
      private void UpdateSelection()
      {
          if (this.linkList == null || this.Links == null) {
              return;
          }
  
          // sync list selection with current source
  
          this.linkList.SelectedItem = this.Links.FirstOrDefault(l =&gt; l.Source == this.SelectedSource);
          SetValue(SelectedLinkGroupProperty, this.Links.FirstOrDefault(l =&gt; l.Children == this.SelectedLinkGroup));
          if (this.Links.FirstOrDefault(l =&gt; l.Children == this.SelectedLinkGroup) != null)
              MessageBox.Show(this.Links.FirstOrDefault(l =&gt; l.Children == this.SelectedLinkGroup).Children.Count.ToString());
      }
  
      /// &lt;summary&gt;
      /// When overridden in a derived class, is invoked whenever application code or internal processes call System.Windows.FrameworkElement.ApplyTemplate().
      /// &lt;/summary&gt;
      public override void OnApplyTemplate()
      {
          base.OnApplyTemplate();
  
          if (this.linkList != null) {
              this.linkList.SelectionChanged -= OnLinkListSelectionChanged;
          }
  
          this.linkList = GetTemplateChild("LinkList") as ListBox;
          if (this.linkList != null) {
              this.linkList.SelectionChanged += OnLinkListSelectionChanged;
          }
  
          UpdateSelection();
      }
  
      private void OnLinkListSelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          var link = this.linkList.SelectedItem as Link;
          if (link != null &amp;&amp; link.Source != this.SelectedSource) {
              SetCurrentValue(SelectedSourceProperty, link.Source);
          }
      }
  
      /// &lt;summary&gt;
      /// Gets or sets the content loader.
      /// &lt;/summary&gt;
      public IContentLoader ContentLoader
      {
          get { return (IContentLoader)GetValue(ContentLoaderProperty); }
          set { SetValue(ContentLoaderProperty, value); }
      }
  
      /// &lt;summary&gt;
      /// Gets or sets a value indicating how the tab should be rendered.
      /// &lt;/summary&gt;
      public TabLayout Layout
      {
          get { return (TabLayout)GetValue(LayoutProperty); }
          set { SetValue(LayoutProperty, value); }
      }
  
      /// &lt;summary&gt;
      /// Gets or sets the collection of links that define the available content in this tab.
      /// &lt;/summary&gt;
      public LinkCollection Links
      {
          get { return (LinkCollection)GetValue(LinksProperty); }
          set { SetValue(LinksProperty, value); }
      }
  
      /// &lt;summary&gt;
      /// Gets or sets the collection of links that define the available content in this tab.
      /// &lt;/summary&gt;
      public LinkCollection SelectedLinkGroup
      {
          get { return (LinkCollection)GetValue(SelectedLinkGroupProperty); }
          set { SetValue(SelectedLinkGroupProperty, value); }
      }
  
      /// &lt;summary&gt;
      /// Gets or sets the collection of links that define the available content in this tab.
      /// &lt;/summary&gt;
      public Link SelectedLink
      {
          get { return (Link)GetValue(SelectedLinkProperty); }
          set { SetValue(SelectedLinkProperty, value); }
      }
  
      /// &lt;summary&gt;
      /// Gets or sets the width of the list when Layout is set to List.
      /// &lt;/summary&gt;
      /// &lt;value&gt;
      /// The width of the list.
      /// &lt;/value&gt;
      public GridLength ListWidth
      {
          get { return (GridLength)GetValue(ListWidthProperty); }
          set { SetValue(ListWidthProperty, value); }
      }
  
      /// &lt;summary&gt;
      /// Gets or sets the source URI of the selected link.
      /// &lt;/summary&gt;
      /// &lt;value&gt;The source URI of the selected link.&lt;/value&gt;
      public Uri SelectedSource
      {
          get { return (Uri)GetValue(SelectedSourceProperty); }
          set { SetValue(SelectedSourceProperty, value); }
      }
  }
  </code></pre>
  
  <p>this causes compilation's errors because of the conflict of base classes(ResourceDictionary and Control)</p>
  
  <p>All XAML:</p>
  
  <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:controls="clr-namespace:FirstFloor.ModernUI.Windows.Controls" x:Class="FirstFloor.ModernUI.Windows.Controls.ModernVerticalMenu"&gt;
  
  &lt;ResourceDictionary.MergedDictionaries&gt;
      &lt;ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/Converters.xaml" /&gt;
  &lt;/ResourceDictionary.MergedDictionaries&gt;
  
  &lt;Style TargetType="controls:ModernVerticalMenu" &gt;
      &lt;Setter Property="FocusVisualStyle" Value="{x:Null}"/&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="controls:ModernVerticalMenu"&gt;
                  &lt;Grid&gt;
                      &lt;Grid.ColumnDefinitions&gt;
                          &lt;ColumnDefinition Width="{TemplateBinding ListWidth}"/&gt;
                          &lt;ColumnDefinition Width="{TemplateBinding ListWidth}"/&gt;
                          &lt;ColumnDefinition Width="auto"/&gt;
                          &lt;ColumnDefinition /&gt;
                      &lt;/Grid.ColumnDefinitions&gt;
  
                      &lt;Border Background="{TemplateBinding BackColor}" Height="{TemplateBinding Height}" BorderThickness="1" BorderBrush="{DynamicResource bordaSuperior}"&gt;
                          &lt;!-- link list --&gt;
                          &lt;ListBox x:Name="LinkList" ItemsSource="{Binding Links, RelativeSource={RelativeSource TemplatedParent}}"  
                                               ScrollViewer.HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" &gt;
  
                              &lt;ListBox.ItemTemplate&gt;
                                  &lt;DataTemplate&gt;
                                      &lt;Grid Height="50" Background="Transparent" Width="500"&gt;
                                          &lt;Border Name="border" Padding="10"&gt;
                                              &lt;Path x:Name="icon" Data="{Binding IconData}" Stretch="Fill" Fill="{DynamicResource Accent}" Width="20" Height="20" HorizontalAlignment="Left" VerticalAlignment="Center" /&gt;
                                          &lt;/Border&gt;
                                          &lt;TextBlock x:Name="texto" ToolTip="{Binding Tooltip}"  Text="{Binding DisplayName}" Margin="45,2,2,2" FontSize="{DynamicResource MediumFontSize}" TextTrimming="CharacterEllipsis" VerticalAlignment="Center" HorizontalAlignment="Left" /&gt;
                                      &lt;/Grid&gt;
                                      &lt;DataTemplate.Triggers&gt;
                                          &lt;DataTrigger Binding="{Binding IconData}" Value="{x:Null}"&gt;
                                              &lt;Setter Property="Margin" TargetName="texto"&gt;
                                                  &lt;Setter.Value&gt;
                                                      &lt;Thickness Bottom="2" Top="2" Left="10" Right="2"/&gt;
                                                  &lt;/Setter.Value&gt;
                                              &lt;/Setter&gt;
                                          &lt;/DataTrigger&gt;
                                          &lt;Trigger Property="IsMouseOver" Value="true"&gt;
                                              &lt;Trigger.Setters&gt;
                                                  &lt;Setter Property="controls:ModernVerticalMenu.BackColor" Value="Red"/&gt;
                                                  &lt;Setter Property="Fill" TargetName="icon"&gt;
                                                      &lt;Setter.Value&gt;
                                                          &lt;SolidColorBrush Color="#f2f2f2" /&gt;
                                                      &lt;/Setter.Value&gt;
                                                  &lt;/Setter&gt;
                                              &lt;/Trigger.Setters&gt;
                                          &lt;/Trigger&gt;
                                      &lt;/DataTemplate.Triggers&gt;
                                  &lt;/DataTemplate&gt;
                              &lt;/ListBox.ItemTemplate&gt;
                          &lt;/ListBox&gt;
                      &lt;/Border&gt;
  
                      &lt;Border x:Name="submenu" Grid.Column="1" Background="{DynamicResource background}" Height="{TemplateBinding Height}" BorderThickness="1" BorderBrush="{DynamicResource bordaSuperior}"&gt;
                          &lt;!-- link list --&gt;
  
                          &lt;ListBox x:Name="list" ItemsSource="{Binding SelectedLinkGroup, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"  
                                               ScrollViewer.HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" &gt;
  
                              &lt;ListBox.ItemTemplate&gt;
                                  &lt;DataTemplate&gt;
                                      &lt;Grid Height="50" Background="Transparent" Width="500"&gt;
                                          &lt;Border Padding="10"&gt;
                                              &lt;Path x:Name="icon" Data="{Binding IconData}" Stretch="Fill" Fill="{DynamicResource Accent}" Width="20" Height="20" HorizontalAlignment="Left" VerticalAlignment="Center" /&gt;
                                          &lt;/Border&gt;
                                          &lt;TextBlock x:Name="texto" ToolTip="{Binding Tooltip}"  Text="{Binding DisplayName}" Margin="45,2,2,2" FontSize="{DynamicResource MediumFontSize}" TextTrimming="CharacterEllipsis" VerticalAlignment="Center" HorizontalAlignment="Left" /&gt;
  
                                      &lt;/Grid&gt;
                                      &lt;DataTemplate.Triggers&gt;
                                          &lt;DataTrigger Binding="{Binding IconData}" Value="{x:Null}"&gt;
                                              &lt;Setter Property="Margin" TargetName="texto"&gt;
                                                  &lt;Setter.Value&gt;
                                                      &lt;Thickness Bottom="2" Top="2" Left="10" Right="2"/&gt;
                                                  &lt;/Setter.Value&gt;
                                              &lt;/Setter&gt;
  
                                          &lt;/DataTrigger&gt;
                                          &lt;Trigger Property="IsMouseOver" Value="true"&gt;
                                              &lt;Setter Property="Fill" TargetName="icon"&gt;
                                                  &lt;Setter.Value&gt;
                                                      &lt;SolidColorBrush Color="#f2f2f2" /&gt;
                                                  &lt;/Setter.Value&gt;
                                              &lt;/Setter&gt;
                                              &lt;!--{Binding RelativeSource={RelativeSource AncestorType=controls:ModernVerticalMenu}, Path=SelectedLinkGroup}--&gt;
                                              &lt;Setter Property="controls:ModernVerticalMenu.SelectedLinkGroup" Value="{Binding Children}"/&gt;
                                          &lt;/Trigger&gt;
                                      &lt;/DataTemplate.Triggers&gt;
                                  &lt;/DataTemplate&gt;
                              &lt;/ListBox.ItemTemplate&gt;
  
                          &lt;/ListBox&gt;
                      &lt;/Border&gt;
  
                      &lt;controls:ModernFrame Grid.Column="2" Source="{Binding SelectedSource, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" ContentLoader="{TemplateBinding ContentLoader}" Margin="10,0,0,0" /&gt;
                  &lt;/Grid&gt;
                  &lt;ControlTemplate.Triggers&gt;
                      &lt;DataTrigger Binding="{Binding SelectedLinkGroup.Count,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}" Value="{x:Null}"&gt;
                          &lt;Setter Property="Visibility" TargetName="submenu" Value="Collapsed"/&gt;
                      &lt;/DataTrigger&gt;
                  &lt;/ControlTemplate.Triggers&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  
  &lt;/Style&gt;
  </code></pre>
  
  <p></p>