Wpf 是否有一种通用方法将鼠标下方ItemContainer的DataContext设置为其他控件的属性?

Wpf 是否有一种通用方法将鼠标下方ItemContainer的DataContext设置为其他控件的属性?,wpf,xaml,Wpf,Xaml,当鼠标悬停在列表项上时,如何将另一个元素的属性设置为列表项的DataContext 我正在尝试创建一个区域,在该区域中,我可以显示鼠标光标下当前项目的预览。我可以使用代码隐藏来实现这一点,但我想找到一种替代方法,可以使用EventSetters/Binding/Triggers/AttachedProperties或任何其他方法 其目的是将该解决方案应用于更松散耦合的场景中,其中ListView控件可以位于单独的资源文件中,并且PreviewControl可以由多个ListView共享,以显示不

当鼠标悬停在列表项上时,如何将另一个元素的属性设置为列表项的DataContext

我正在尝试创建一个区域,在该区域中,我可以显示鼠标光标下当前项目的预览。我可以使用代码隐藏来实现这一点,但我想找到一种替代方法,可以使用EventSetters/Binding/Triggers/AttachedProperties或任何其他方法

其目的是将该解决方案应用于更松散耦合的场景中,其中ListView控件可以位于单独的资源文件中,并且PreviewControl可以由多个ListView共享,以显示不同类型的预览

以下代码可以工作,但需要代码隐藏:

<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>
解决方案1(不是通用的,但简单且有效):

将已经实现的逻辑封装到自定义控件中,该控件具有一个新的依赖属性(typeof(object)),该属性表示HoveredItemContext。在自定义列表视图的构造函数中,可以创建ContainerStyle并附加事件。在此EventHandlers中设置HoveredItemContext,您可以从外部绑定到此属性:

 <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}}" />
解决方案2(更通用):


我仍在处理类似的问题,但尚未完成;)如果我把它结束,我会把它贴在这里。

谢谢,我喜欢这个主意,我想我可以用它。公开dependency属性比像我在示例中所做的那样尝试从事件处理程序将datacontext推送到预览控件上感觉更好。也可以使用附加属性,而无需创建自定义控件。
 <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}}" />
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;
            })));
    }
}