C# 绑定复选框命令

C# 绑定复选框命令,c#,wpf,xaml,mvvm,prism,C#,Wpf,Xaml,Mvvm,Prism,我对listview中的每一行都有一个复选框。我想将复选框OnClick绑定到视图模型中的我的命令。但这不符合我的命令。我该怎么办?下面是我的xaml和Viewmodel Xaml: 视图模型: public class MyViewModel { private List<Demo> lstdemo; private ObservableCollection<Demo> Obcollection; privat

我对listview中的每一行都有一个复选框。我想将复选框OnClick绑定到视图模型中的我的命令。但这不符合我的命令。我该怎么办?下面是我的xaml和Viewmodel

Xaml:


视图模型:

public class MyViewModel
    {
        private List<Demo> lstdemo;
        private ObservableCollection<Demo> Obcollection;
        private SampleDb db;
        public MyViewModel()
        {
            db = new SampleDb();
            lstdemo = db.Demoes.ToList();
            Convert();

        }

        public void Convert()
        {
            Obcollection = new ObservableCollection<Demo>(lstdemo);
        }

        public ObservableCollection<Demo> obcollection
        {
            get { return Obcollection; }
            set { Obcollection = value; }
        }

        private ICommand mUpdater;
        public ICommand UpdateCommand
        {
            get
            {
                if (mUpdater == null)
                    mUpdater = new Updater();
                return mUpdater;
            }
            set
            {
                mUpdater = value;
            }
        }

        public class Updater : ICommand
        {
            #region ICommand Members

            public bool CanExecute(object parameter)
            {

                return true;
            }

            public event EventHandler CanExecuteChanged;

            public void Execute(object parameter)
            {
                if (parameter == null)
                {
                    MessageBox.Show("Hello");
                }

            }

            #endregion
        }

    }
公共类MyViewModel
{
私人名单;
私人可观测集合;
私有样本数据库;
公共MyViewModel()
{
db=新的SampleDb();
lstdemo=db.demos.ToList();
Convert();
}
公共void Convert()
{
Obcollection=新的ObservableCollection(lstdemo);
}
公共可观测集合
{
获取{return Obcollection;}
设置{Obcollection=value;}
}
私人ICommand mUpdater;
公共ICommand更新命令
{
得到
{
if(mUpdater==null)
mUpdater=新的更新程序();
返回mUpdater;
}
设置
{
mUpdater=值;
}
}
公共类更新程序:ICommand
{
#区域ICommand成员
公共布尔CanExecute(对象参数)
{
返回true;
}
公共事件处理程序CanExecuteChanged;
public void Execute(对象参数)
{
if(参数==null)
{
MessageBox.Show(“你好”);
}
}
#端区
}
}

DataTemplate
中的
DataContext
隐式地是(列表视图的)当前项。因此,在这种情况下,您必须显式地为
绑定设置源(或相对源)。您可以使用
RelativeSource
查找父级
ListView
,如下所示:

<CheckBox Name="chk" IsChecked="{Binding IsChecked,
                                 Mode=TwoWay,NotifyOnTargetUpdated=True}" 
          Command="{Binding Path=DataContext.UpdateCommand, 
                    RelativeSource={RelativeSource AncestorType=ListView}}"/>

请注意
路径的更改。源现在是
列表视图
,因此
UpdateCommand
的路径是
DataContext.UpdateCommand

<CheckBox Name="chk" IsChecked="{Binding IsChecked,
                                 Mode=TwoWay,NotifyOnTargetUpdated=True}" 
          Command="{Binding Path=DataContext.UpdateCommand, 
                    RelativeSource={RelativeSource AncestorType=ListView}}"/>