Solution 1 (not that generic, but simple and works):
encapsulate already implemented logic in a user control that has a new dependency property (typeof (object)) that represents the HoveredItemContext. In the constructor of your custom list, you can create a ContainerStyle and attach events. Set the HoveredItemContext in this EventHandlers, and you can bind 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 is the user 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 general):
I am still working on this on a similar issue, but it is not finished yet;) If I bring it, I will post it here.