在WPF MVVM +使用Command + CommandParameter在列表视图中删除一行 [英] Deleting row in listview using Command + CommandParameter in WPF + MVVM

查看:514
本文介绍了在WPF MVVM +使用Command + CommandParameter在列表视图中删除一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除使用Command和CommandParameter像下面的ListView控件中的一行。

 < GridViewColumn标题=X>
< GridViewColumn.CellTemplate>
    <&DataTemplate的GT;
        <&StackPanel的GT;
           < TextBlock的文本={结合CriteriaId}/>
           <按钮名称=btnDeleteCriterion标签={结合CriteriaId}CONTENT ={结合CriteriaId}前景=红粗细=大胆
                                                             命令={结合DeleteCriterionCommand}
                                                                        的DataContext ={绑定的DataContext,的RelativeSource = {的RelativeSource FindAncestor,AncestorType =的ListView}}
                                                             CommandParameter ={绑定的RelativeSource = {自我的RelativeSource},路径=标签}
                                                                        />
        <&StackPanel的GT;
    < / DataTemplate中>
< /GridViewColumn.CellTemplate>

我试图抓住按钮的Tag属性,并把它传递给命令像上面比像这样从列表中删除。


  

编辑,上面XAML和补充,它使用相同的结合作为按钮的标签和内容一个TextBlock,但不知何故按钮没有得到的价值,但TextBlock的呢!?


 公共无效DeleteCriterion(对象criterionId)
    {
        INT crtId =(INT)criterionId;
        标准的crt = _criteria.FirstOrDefault((C)=> c.CriterionId == crtId);
        如果(CRT!= NULL)
            _criteria.Remove(CRT);
    }

但我总是criterionId参数为空。

我在做什么错了?


解决方案

既然你明确地在按钮中设置的DataContext,当你做这样 {结合SomeProperty} 就会认为SomeProperty是你刚才设置的DataContext的。尝试使用更明确样结合:

 {绑定的RelativeSource = {的RelativeSource AncestorType =的StackPanel},路径= DataContext.CriteriaId}

这将给你正确的DataContext的 CriteriaID 喜欢它是的TextBlock

I want to delete a row in ListView Control using Command and CommandParameter like below.

<GridViewColumn Header="X">
<GridViewColumn.CellTemplate>
    <DataTemplate>
        <StackPanel>
           <TextBlock Text="{Binding CriteriaId}"/>
           <Button Name="btnDeleteCriterion" Tag="{Binding CriteriaId}" Content="{Binding CriteriaId}" Foreground="Red" FontWeight="Bold" 
                                                             Command="{Binding DeleteCriterionCommand}" 
                                                                        DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}"
                                                             CommandParameter="{Binding RelativeSource={RelativeSource self}, Path=Tag}"
                                                                        />
        <StackPanel>
    </DataTemplate>
</GridViewColumn.CellTemplate>

I am trying to grab the Tag property of the Button and pass it to Command like above and than remove it from list like so.

Edited, above XAML and added a TextBlock which uses the same binding as the Button's Tag and Content, but somehow Button doesn't get the value but TextBlock does!?

    public void DeleteCriterion(object criterionId)
    {
        int crtId = (int)criterionId;
        Criterion crt = _criteria.FirstOrDefault((c) => c.CriterionId == crtId);
        if (crt != null)
            _criteria.Remove(crt);
    }

but I always get criterionId parameter as null.

What am I doing wrong?

解决方案

Since you're explicitly setting the DataContext within the button, when you're doing a binding like {Binding SomeProperty} it will assume that SomeProperty is in the DataContext that you just set. Try using a more explicit binding like:

"{Binding RelativeSource={RelativeSource AncestorType=StackPanel}, Path=DataContext.CriteriaId}" 

which will give you the correct DataContext's CriteriaID like it is for the TextBlock.

这篇关于在WPF MVVM +使用Command + CommandParameter在列表视图中删除一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆