C# EventToCommand祖先绑定WPF

C# EventToCommand祖先绑定WPF,c#,wpf,C#,Wpf,调用时出现问题 <i:Interaction.Triggers> <i:EventTrigger EventName="MouseDoubleClick"> <Command:EventToCommand Command="{Binding Path=Test, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ViewModel:ListViewModel}}}" /

调用时出现问题

 <i:Interaction.Triggers>
 <i:EventTrigger EventName="MouseDoubleClick">
 <Command:EventToCommand Command="{Binding Path=Test, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ViewModel:ListViewModel}}}" />
  </i:EventTrigger>
  </i:Interaction.Triggers>

System.Windows.Data错误:4:找不到与绑定的源 参考“相对资源查找器”, AncestorType='PhotoBrowser.List.ViewModel.ListViewModel', AncestorLevel='1'。BindingExpression:Path=Test;DataItem=null;目标 元素是“EventToCommand”(HashCode=37598799);目标属性为 “命令”(键入“ICommand”)

如何修复它? ListViewModel:

namespace PhotoBrowser.List.ViewModel
{
    public class ListViewModel : ViewModelBase
    {

        public ObservableCollection<Item> Items { get; set; }

        public ListViewModel()
        {
            Items = new ObservableCollection<Item>();
        }
        public RelayCommand Test
        {
            get;
            set;
        }
        public RelayCommand DoubleClickOnItem
        {
            get
            {
                return new RelayCommand(() => OpenEdit());
            }
        }

        private void OpenEdit()
        {
            Messenger.Default.Send(new NotificationMessage("Edit"));
        }

        public RelayCommand<DragEventArgs> Drop
        {
            get
            {
                return new RelayCommand<DragEventArgs>(CheckInstanceCertificate);
            }
        }

        private void CheckInstanceCertificate(DragEventArgs args)
        {
            var path = string.Join("", (string[])args.Data.GetData(DataFormats.FileDrop, true));
            Items.Add(new Item { ImagePath = path });
        }
    }

}
namespace PhotoBrowser.List.ViewModel
{
公共类ListViewModel:ViewModelBase
{
公共ObservableCollection项{get;set;}
公共ListViewModel()
{
Items=新的ObservableCollection();
}
公共中继命令测试
{
得到;
设置
}
公共Relay Command Double ClickOnItem
{
得到
{
返回新的RelayCommand(()=>OpenEdit());
}
}
私有void OpenEdit()
{
Messenger.Default.Send(新建通知消息(“编辑”);
}
公共交通支路
{
得到
{
返回新的RelayCommand(检查InstanceCertificate);
}
}
私有无效检查实例证书(DragEventArgs参数)
{
var path=string.Join(“,(string[])args.Data.GetData(DataFormats.FileDrop,true));
添加(新项{ImagePath=path});
}
}
}
ListView.xaml

<UserControl x:Class="PhotoBrowser.List.View.ListView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
             xmlns:ViewModel="clr-namespace:PhotoBrowser.List.ViewModel"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             DataContext="{Binding Source={StaticResource Locator}, Path=List}"
             >
    <UserControl.Resources>
        <Style x:Key="FileItemStyle" TargetType="{x:Type ListViewItem}">
            <Setter Property="Margin" Value="5,5,5,5"/>
            <Setter Property="Padding" Value="0,0,0,0"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate  TargetType="{x:Type ListViewItem}">
                        <Grid HorizontalAlignment="Left" VerticalAlignment="Top" Height="50" >
                            <Border x:Name="border" BorderBrush="{x:Null}" BorderThickness="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CornerRadius="2.5"/>
                            <StackPanel HorizontalAlignment="Stretch"  VerticalAlignment="Stretch">
                                <ContentPresenter/>
                            </StackPanel>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <Grid>
        <ListView ItemsSource="{Binding Items}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
              ItemContainerStyle="{StaticResource FileItemStyle}" AllowDrop="True">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Drop">
                    <Command:EventToCommand Command="{Binding Drop}" PassEventArgsToCommand="True" />
                </i:EventTrigger>
            </i:Interaction.Triggers>

            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel/>
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>

            <ListView.ItemTemplate>
                <DataTemplate>
                    <DockPanel>
                        <Image Source="{Binding ImagePath}" Height="64" Width="64">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="MouseDoubleClick">
                                    <Command:EventToCommand Command="{Binding Path=Test, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ViewModel:ListViewModel}}}" />
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </Image>
                    </DockPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</UserControl>

看看:

AncestorType={x:Type ViewModel:ListViewModel}
ViewModel
不是祖先,因为
ViewModel
在视图中没有控件。正确答案是:

  • 列表视图(父级)
    • ListViewItem(子项)
您应该绑定到ListView的DataContext(因为这是您的
ViewModel
):


看看:

AncestorType={x:Type ViewModel:ListViewModel}
ViewModel
不是祖先,因为
ViewModel
在视图中没有控件。正确答案是:

  • 列表视图(父级)
    • ListViewItem(子项)
您应该绑定到ListView的DataContext(因为这是您的
ViewModel
):