C# wpf InvokeCommandAction绑定

C# wpf InvokeCommandAction绑定,c#,wpf,C#,Wpf,我在装订方面有一些问题。我不知道怎么做 using System; using System.Collections.Generic; using System.Linq; using System.Text; using GalaSoft.MvvmLight; using ManyViews.Model; using System.Collections.ObjectModel; using System.Windows.Input; using GalaSoft.MvvmLight.Comm

我在装订方面有一些问题。我不知道怎么做

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using ManyViews.Model;
using System.Collections.ObjectModel;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;

namespace ManyViews.ViewModel
{
    class ListViewModel : ViewModelBase
    {
        public void show()
        {
            System.Windows.MessageBox.Show("Raise");
        }
        public ListViewModel()
        {
            EventChecked = new RelayCommand(() => show());
        }
        public ICommand EventChecked { get; set; }
        public ObservableCollection<ListStruct> ShowList
        {
            get { return ListM.Items; }
            set { ListM.Items = value; RaisePropertyChanged("ShowList"); }
        }
        ListModel ListM = new ListModel();
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用GalaSoft.MvvmLight;
使用manyView.Model;
使用System.Collections.ObjectModel;
使用System.Windows.Input;
使用GalaSoft.MvvmLight.Command;
名称空间manyView.ViewModel
{
类ListViewModel:ViewModelBase
{
公开展览(
{
System.Windows.MessageBox.Show(“Raise”);
}
公共ListViewModel()
{
EventChecked=newrelaycommand(()=>show());
}
公共ICommand事件检查{get;set;}
公共可观测集合显示列表
{
获取{return ListM.Items;}
set{ListM.Items=value;RaisePropertyChanged(“ShowList”);}
}
ListModel ListM=新的ListModel();
}
}
如果需要选择另一个上下文,我总是使用静态资源,但在本例中,它在EventTrigger中没有属性数据上下文

<UserControl x:Class="ManyViews.View.List"
         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="http://schemas.microsoft.com/expression/2010/interactivity" 
         xmlns:test="clr-namespace:ManyViews.ViewModel"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
    <test:ListViewModel x:Key="Test"></test:ListViewModel>
</UserControl.Resources>
<Grid>
    <ListView ItemsSource="{Binding ShowList}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding name}">
                    <i:Interaction.Triggers >
                        <i:EventTrigger EventName="Checked">
                            <i:InvokeCommandAction  Command="{Binding DataContext.EventChecked, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type test:ListViewModel}}}"></i:InvokeCommandAction>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </CheckBox>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

在这种情况下,我有一个错误

“System.Windows.Data错误:4:找不到与绑定的源 参考“相对资源查找器”, AncestorType='ManyView.ViewModel.ListViewModel',AncestorLevel='1'。 BindingExpression:Path=DataContext.EventChecked;DataItem=null;目标 元素为“InvokeCommandAction”(HashCode=43550996);目标属性 是“命令”(类型为“ICommand”)


AncestorType={x:Type-test:ListViewModel}
是您的问题

可视化树中没有比当前项高的
test:ListViewModel
s。只有不同的控件,而不是(视图)模型

将其更改为搜索
列表视图

<i:InvokeCommandAction  
     Command="{Binding DataContext.EventChecked, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}}">
</i:InvokeCommandAction>