Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7042339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:09:23+00:00 2026-05-28T02:09:23+00:00

I created a simple, stripped ListView style that highlights an element when the IsMouseOver

  • 0

I created a simple, stripped ListView style that highlights an element when the IsMouseOver property is true. This is done by triggering in the ItemContainerStyle. This works great and the xaml is like this:

<ListView>

  <ListView.ItemTemplate>
    <DataTemplate>
      <!--UserControl with actual content goes here-->
     </DataTemplate>
   </ListView.ItemTemplate>

   <ListView.ItemContainerStyle>
     <Style TargetType="{x:Type ListViewItem}">

       <Setter Property="Template">
         <Setter.Value>
           <ControlTemplate TargetType="ListViewItem">
             <!--here is a  border with the ContentPresenter inside-->
           </ControlTemplate>
         </Setter.Value>
       </Setter>

       <Style.Triggers>
         <Trigger Property="IsMouseOver" Value="True">
           <Setter Property="Background" Value="Lime"/>                                         
         </Trigger>
      </Style.Triggers>

    </Style>
  <ListView.ItemContainerStyle>
</ListView>

However I would also like that the color set on hovering stays when the actual element’s contextmenu is shown by right-clicking it. Basically the question is like this one, except that I cannot use the (otherwise great) answer there: the idea is to add a trigger to check when the contextmenu is open:

<DataTrigger Binding="{Binding ContextMenu.IsOpen}" Value="True">
  <Setter Property="Background" Value="Lime"/>
</DataTrigger>

The question is: what binding expression do I enter in order to figure out that ContextMenu.IsOpen on the actual content set in the DataTemplate? I tried all sort of things like referring to ContentPresenter.ContextMenu.IsOpen etc but none worked.

Apart from using ContextMenu.IsOpen, I already tried tons of combinations of triggers on IsSelected, event triggers on MouseLeave etc but also to no avail. So the second question is: if the contextmenu trick does not work, is there another way to get this effect? Basically I want a list view that does not support selecting of any kind, but does show the user at which element the mouse is, no matter is a menu is partly hiding it or not.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:09:23+00:00Added an answer on May 28, 2026 at 2:09 am

    As uausal when something seems hard in xaml, this was perfectly solvable using attached properties with the additional bonus of being reuasable. The basic principle is to attach a behavior to a FrameworkElement, and hook it’s MouseEneter/Leave events. Apart from those, also look for any children having a contextmenu and hook the ContextMenuOpening/Closing events. I don’t have a blog or repository so here is the code, I imagine this can be useful for others as well.

    public static class HasMouseOver
    {
      private static readonly DependencyProperty HasMouseOverBehaviorProperty =        DependencyProperty.RegisterAttached(
              "HasMouseOverBehavior", typeof( HasMouseOverBehavior ),
              typeof( FrameworkElement ), null );
    
      private static void AttachBehavior( FrameworkElement target )
      {
        var behavior = target.GetValue( HasMouseOverBehaviorProperty ) as HasMouseOverBehavior;
        if( behavior == null )
        {
          behavior = new HasMouseOverBehavior( target, HasMouseProperty );
          target.SetValue( HasMouseOverBehaviorProperty, behavior );
        }
      }
    
      private static void DetachBehavior( FrameworkElement target )
      {
        target.ClearValue( HasMouseOverBehaviorProperty );
      }
    
      public static readonly DependencyProperty RegisterProperty = DependencyProperty.RegisterAttached(
              "Register", typeof( bool ),
              typeof( HasMouseOver ), new PropertyMetadata( false, RegisterPropertyChanged ) );
    
      public static void SetRegister( FrameworkElement element, bool value )
      {
        element.SetValue( RegisterProperty, value );
      }    
      public static bool GetRegister( FrameworkElement element )
      {
        return (bool) element.GetValue( RegisterProperty );
      }
    
      private static void RegisterPropertyChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
      {
        var target = d as FrameworkElement;
        if( target == null )
          return;
        if( (bool) e.NewValue )
          AttachBehavior( target );
        else
          DetachBehavior( target );
      }
    
      public static readonly DependencyProperty HasMouseProperty = DependencyProperty.RegisterAttached(
          "HasMouse", typeof( bool ),
          typeof( HasMouseOver ), null );
    
      public static void SetHasMouse( FrameworkElement element, bool value )
      {
        element.SetValue( HasMouseProperty, value );
      }    
      public static bool GetHasMouse( FrameworkElement element )
      {
        return (bool) element.GetValue( HasMouseProperty );
      }
    }
    
    public class HasMouseOverBehavior
    {
      private readonly DependencyProperty dep;
      private readonly FrameworkElement target;
      private bool isReallyLeaving;
    
      public HasMouseOverBehavior( FrameworkElement target, DependencyProperty dep )
      {
        this.target = target;
        this.dep = dep;
        target.Loaded += Loaded;
        target.Unloaded += Unloaded;
        target.MouseEnter += MouseEnter;
        target.MouseLeave += MouseLeave;
      }
    
      private void Loaded( object sender, RoutedEventArgs e )
      {
        var childrenWithMenu = target.FindChildren<FrameworkElement>( u => u.ContextMenu != null );
        foreach( var child in childrenWithMenu )
        {
          child.ContextMenuOpening += ContextMenuOpening;
          child.ContextMenuClosing += ContextMenuClosing;
        }
      }
    
      private void Unloaded( object sender, RoutedEventArgs e )
      {
        var childrenWithMenu = target.FindChildren<FrameworkElement>( u => u.ContextMenu != null );
        foreach( var child in childrenWithMenu )
        {
          child.ContextMenuOpening -= ContextMenuOpening;
          child.ContextMenuClosing -= ContextMenuClosing;
        }
      }
    
      private void ContextMenuOpening( object sender, ContextMenuEventArgs e )
      {
        isReallyLeaving = false;
      }
    
      private void ContextMenuClosing( object sender, ContextMenuEventArgs e )
      {
        if( !isReallyLeaving )  //else, mouse is still over element eg upon Esc.
          DoesNotHaveMouse();
      }
    
      private void MouseEnter( object sender, System.Windows.Input.MouseEventArgs e )
      {
        isReallyLeaving = true;
        HasMouse();
      }
    
      private void MouseLeave( object sender, System.Windows.Input.MouseEventArgs e )
      {
        if( isReallyLeaving )
        {
          isReallyLeaving = false;
          DoesNotHaveMouse();
        }
      }
    
      private void HasMouse()
      {
        target.SetValue( dep, true );
      }
    
      private void DoesNotHaveMouse()
      {
        target.SetValue( dep, false );
      }
    }
    

    And in xaml:

    <style>
      <Setter Property="behav:HasMouseOver.Register" Value="True"/>
      <Style.Triggers>
        <Trigger Property="behav:HasMouseOver.HasMouse" Value="True">
          ...
        </Trigger>
      </Style.Triggers>
    </style>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok I created simple user control that look like this <%@ Control Language=C# AutoEventWireup=true
Could someone help me on this, I have created simple web services using axis2
I created simple HttpListener that listens to port 9090 and depending on a request's
How to manually create Friendly URLs? (PHP) So I have created simple php file
I created a simple .NET windows application in Visual Studio 2005 and on just
I created a simple dialog-based application, and in the default CDialog added three buttons
I created a simple detail edit form earlier, and decided to data bind some
I created a simple PHP site for a friend last year. I went to
I created a simple Converter to concatenate the text of four TextBoxes in my
I created a simple Oracle type: create or replace TYPE MY_TYPE AS OBJECT (ID

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.