如何将ListView DataTemplate中的按钮绑定到ViewModel中的命令? (MVVMLight) [英] How to bind buttons in ListView DataTemplate to Commands in ViewModel? (MVVMLight)

查看:88
本文介绍了如何将ListView DataTemplate中的按钮绑定到ViewModel中的命令? (MVVMLight)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下代码的列表视图:

I have a list view with following code:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <ListBox x:Name="allToDoItemsListBox" 
             ItemsSource="{Binding AllToDoItems,Mode=OneWay}" 
             Margin="12,0,12,0" 
             Width="440" 
             ItemTemplate="{StaticResource ToDoListBoxItemTemplate}" />
</Grid>

数据模板如下:

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="ToDoListBoxItemTemplate">

        <Grid HorizontalAlignment="Stretch" Width="420">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="100" />
            </Grid.ColumnDefinitions>

            <CheckBox 
                IsChecked="{Binding IsComplete, Mode=TwoWay}" 
                Grid.Column="0" VerticalAlignment="Top"/>

            <TextBlock 
                Text="{Binding ItemName}" 
                FontSize="{StaticResource PhoneFontSizeLarge}" 
                Foreground="Gray"
                Grid.Column="1" Grid.ColumnSpan="2" 
                VerticalAlignment="Top" Margin="-36, 12, 0, 0"/>

            <Button                                
                Grid.Column="3"
                x:Name="deleteTaskButton"
                BorderThickness="0"                                                                  
                Margin="0, -18, 0, 0"
                Command="{Binding Path=DeleteCommand}" CommandParameter="{Binding}"/>

            <Image 
                Source="/Images/appbar.delete.rest.png"
                Height="75"
                Width="75"/>
        </Grid>
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>

我试图将按钮命令"DeleteCommand"绑定到ViewModel中的ICommand,但是它失败了我该怎么办?

I tried to bind the Button Command "DeleteCommand" to a ICommand in ViewModel but it failed what should i do?

ViewModel中的代码为:

The Code in ViewModel is :

 public ICommand DeleteCommand { get; private set; }

在ViewModel构造中:

In the ViewModel Construct :

 DeleteCommand = new RelayCommand<object>(Delete);

和删除方法:

private void Delete(object obj)
{
    ToDoItem newToDoItem = obj as ToDoItem;
    DeleteToDoItem(newToDoItem);
}

当按下列表中每个项目的相应删除按钮时,我需要将该项目作为参数传递给DeleteToDoItem()方法,但命令在此处不触发我该怎么办?

I need to pass the item to DeleteToDoItem() method as parameter when the corresponding delete button of each item in the list is pressed but the command does not fire here what should i do?

推荐答案

在ViewModel中,您要将ToDoItem传递给Delete命令.

In your ViewModel you want to pass a ToDoItem to your Delete command.

DeleteCommand = new RelayCommand<ToDoItem>(DeleteToDoItem);

您的DataTemplate的DataContext与ListView的不同.

And your DataTemplate's DataContext is different from the ListView's.

<Button Command="{Binding ElementName=allToDoItemsListBox, 
                          Path=DataContext.DeleteCommand}" 
        CommandParameter="{Binding}" />

应该这样做.

这篇关于如何将ListView DataTemplate中的按钮绑定到ViewModel中的命令? (MVVMLight)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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