WPF:如何使用 MVVM 将命令绑定到 ListBoxItem? [英] WPF: How to bind a command to the ListBoxItem using MVVM?

查看:122
本文介绍了WPF:如何使用 MVVM 将命令绑定到 ListBoxItem?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习 MVVM.我按照这个 MVVM 教程(我高度向所有 MVVM 初学者推荐它).基本上,到目前为止我已经创建了几个文本框,用户可以在其中添加他或她的数据,一个保存该数据的按钮,随后将所有条目填充到 ListBox 中.

I have just started learning MVVM. I've made the application from scratch by following this MVVM tutorial (I highly recommend it to all MVVM beginners out there). Basically, what I have created so far is a couple of text boxes where user adds his or her data, a button to save that data which subsequently populates the ListBox with all entries made.

这里是我卡住的地方:我希望能够双击 ListBoxItem 并触发我创建并添加到我的 ViewModel 的命令.我不知道如何完成 XAML 端,即我不知道如何将该命令绑定到 ListBox(Item).

Here's where I got stuck: I want to be able to double-click on a ListBoxItem and to trigger a command that I have created and added to my ViewModel. I don't know how to finish the XAML side, i.e. I don't know how to bind that command to the ListBox(Item).

这是 XAML:

...
<ListBox 
    Name="EntriesListBox" 
    Width="228" 
    Height="208" 
    Margin="138,12,0,0" 
    HorizontalAlignment="Left" 
    VerticalAlignment="Top" 
    ItemsSource="{Binding Entries}" />
...

这是视图模型:

public class MainWindowViewModel : DependencyObject
{
    ...
    public IEntriesProvider Entries
    {
        get { return entries; }
    }

    private IEntriesProvider entries;
    public OpenEntryCommand OpenEntryCmd { get; set; }

    public MainWindowViewModel(IEntriesProvider source)
    {
        this.entries = source;
        ...
        this.OpenEntryCmd = new OpenEntryCommand(this);
    }
    ...
}

最后,这是我想在用户双击 EntriesListBox 中的项目后执行的 OpenEntryCommand:

And finally, here's the OpenEntryCommand that I want to be executed once the user double-clicks the item in the EntriesListBox:

public class OpenEntryCommand : ICommand
{
    private MainWindowViewModel viewModel;

    public OpenEntryCommand(MainWindowViewModel viewModel)
    {
        this.viewModel = viewModel;
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public bool CanExecute(object parameter)
    {
        return parameter is Entry;
    }

    public void Execute(object parameter)
    {
        string messageFormat = "Subject: {0}
Start: {1}
End: {2}";
        Entry entry = parameter as Entry;
        string message = string.Format(messageFormat, 
                                       entry.Subject, 
                                       entry.StartDate.ToShortDateString(), 
                                       entry.EndDate.ToShortDateString());

        MessageBox.Show(message, "Appointment");
    }
}

请帮忙,我将不胜感激.

Please help, I'd appreciate it.

推荐答案

不幸的是,只有 ButtonBase 派生控件可以将 ICommand 对象绑定到它们的 Command 属性(用于 Click 事件).

Unfortunately, only ButtonBase derived controls have the possibility for binding ICommand objects to their Command properties (for the Click event).

但是,您可以使用 Blend 提供的 API 将事件(例如在您的情况下 ListBox 上的 MouseDoubleClick)映射到 ICommand 对象.

However, you can use an API provided by Blend to map an event (like in your case MouseDoubleClick on the ListBox) to an ICommand object.

<ListBox>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseDoubleClick">
            <i:InvokeCommandAction Command="{Binding YourCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ListBox>

您必须定义:xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 并引用 System.Windows.Interactivity.dll.

You'll have to define: xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" and have a reference to System.Windows.Interactivity.dll.

-- 编辑--这是 WPF4 的一部分,但如果您不使用 WPF4,您可以使用 Microsoft.Windows.Interactivity.这个 dll 来自 Blend SDK,它不需要 Blend,来自这里:http://www.microsoft.com/downloads/en/details.aspx?FamilyID=f1ae9a30-4928-411d-970b-e682ab179e17&displaylang=en

-- EDIT -- This is part of WPF4, but u can use Microsoft.Windows.Interactivity if you're not using WPF4. This dll is from Blend SDK, which doesn't require Blend, from here: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=f1ae9a30-4928-411d-970b-e682ab179e17&displaylang=en

更新:我发现了一些应该对您有所帮助的内容.检查 这个链接在 MVVM Light Toolkit 上,其中包含有关如何执行此操作的演练以及 链接到所需的库.MVVM Light Toolkit 是一个非常有趣的框架,用于在 Silverlight、WPF 和 WP7 中应用 MVVM.

Update: I found something that should help you. check this link on MVVM Light Toolkit which contains a walkthrough on how to do this, along with a link to the needed libraries. MVVM Light Toolkit is a very interesting framework for applying MVVM with Silverlight, WPF, and WP7.

希望这有帮助:)

这篇关于WPF:如何使用 MVVM 将命令绑定到 ListBoxItem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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