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 989897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:51:51+00:00 2026-05-16T05:51:51+00:00

When hovering over an item in a list, how can I set a property

  • 0

When hovering over an item in a list, how can I set a property on another element to the DataContext of the list item?

I’m trying to make an area where I can display a preview of the item currently underneath the mouse cursor. I’m able to do this using code-behind, but I’d like to find an alternative way which could use EventSetters/Binding/Triggers/AttachedProperties or any other means.

The aim is to apply the solution in a more loosely coupled scenario where the ListView control could be in separate resource file and the PreviewControl might be shared by a number of ListViews to show previews of different types.

The following piece of code works, but requires code-behind:

<Window x:Class="Previewer.PreviewWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="PreviewWindow" Height="300" Width="300">
<Window.Resources>
    <x:Array x:Key="Data" Type="sys:String">
        <sys:String>First</sys:String>
        <sys:String>Second</sys:String>
    </x:Array>

    <CollectionViewSource x:Key="DataSource" Source="{StaticResource Data}"/>
</Window.Resources>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <ContentControl Grid.Row="0" x:Name="PreviewControl"/>

    <ListView Grid.Row="1" ItemsSource="{Binding Source={StaticResource DataSource}}">
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <EventSetter Event="MouseEnter" Handler="ListViewItem_MouseEnter"/>
                <EventSetter Event="MouseLeave" Handler="ListViewItem_MouseLeave"/>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>
</Grid>

The code-behind which at the moment does the trick:

public partial class PreviewWindow : Window
{
    public PreviewWindow()
    {
        InitializeComponent();
    }

    private void ListViewItem_MouseEnter(object sender, MouseEventArgs e)
    {
        var listViewItem = (ListViewItem)sender;
        PreviewControl.Content= listViewItem.DataContext;
    }

    private void ListViewItem_MouseLeave(object sender, MouseEventArgs e)
    {
        var listViewItem = (ListViewItem)sender;
        PreviewControl.Content= null;
    }
}
  • 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-16T05:51:52+00:00Added an answer on May 16, 2026 at 5:51 am

    Solution 1 (not that generic but simple and works):

    encapsulate the logic you already implemented into a custom control, which has a new dependency property (typeof(object)), which represents the HoveredItemContext. In the constructor of your custom list view, you can create the ContainerStyle and attach the events. Set the HoveredItemContext in this EventHandlers and you can bind to this property from the outside:

     <ContentControl Grid.Row="0" x:Name="PreviewControl" 
         Content="{Binding ElementName=MyListView, Path=HoveredItemContext}"/>
     <local:MyListView Grid.Row="1" x:Name="MyListView" 
         ItemsSource="{Binding Source={StaticResource DataSource}}" />
    

    And here the custom control (works):

    public class MyListView : ListView
    {
        public static readonly DependencyProperty HoveredItemContextProperty = DependencyProperty.Register(
            "HoveredItemContext",
            typeof(object),
            typeof(MyListView),
            new PropertyMetadata(null));
    
        public object HoveredItemContext
        {
            get { return GetValue(HoveredItemContextProperty); }
            set { SetValue(HoveredItemContextProperty, value); }
        }
    
        public MyListView()
        {
            this.ItemContainerStyle = new Style()
            {
                TargetType = typeof(ListViewItem),
            };
    
            this.ItemContainerStyle.Setters.Add(new EventSetter(ListViewItem.MouseEnterEvent,
                (MouseEventHandler)((s, e) =>
                {
                    this.HoveredItemContext = (s as ListViewItem).DataContext;
                })));
    
            this.ItemContainerStyle.Setters.Add(new EventSetter(ListViewItem.MouseLeaveEvent,
                (MouseEventHandler)((s, e) =>
                {
                    this.HoveredItemContext = null;
                })));
        }
    }
    

    Solution 2 (more generic):

    I am still working on it on a similar problem but its not yet finished 😉 If i bring it to an end, i will post this here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a list item element that contains a child anchor element. <ul id=machine-list>
I have a small tabbed navigation setup using CSS. When hovering over the tabs
I am trying to stop the animation of my jcarouselled item when the user
I come from a .Net world so I'm used to just hovering over a
I'm using an asp:LinkButton. The text underlines upon hovering over it. However I want
I have recently added jQuery to a site of mine. When hovering over sections
I'm trying to use jQuery and RaphaelJS to: Create circles Display some information when
C#: Is there a shortcut to detect if mouse is still hovering on top
So I am trying to create a drop down menu using .hover() and .toggle().
In my menu below, I have this in my CSS: ul li ul {display:none;}

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.