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

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

问题描述

我刚开始学习MVVM。我已经按照此 MVVM教程做出从头开始应用(我强烈推荐给所有初学者MVVM在那里)。基本上,我到目前为止创建一对夫妇的文本框,其中用户将他或她的数据,一键拯救这些数据随后与填充的所有条目列表框。

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中双击并触发我已创建并添加到我的视图模型的命令。我不知道如何完成XAML的一面,即我不知道如何该命令绑定到列表框(项目)。

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}\nStart: {1}\nEnd: {2}";
        Entry entry = parameter as Entry;
        string message = string.Format(messageFormat, 
                                       entry.Subject, 
                                       entry.StartDate.ToShortDateString(), 
                                       entry.EndDate.ToShortDateString());

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

请帮忙,我想AP preciate它。

Please help, I'd appreciate it.

推荐答案

不幸的是,只有 ButtonBase 派生的控件具有约束力的可能性的ICommand 对象的命令属性(在点击事件)。

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

不过,您可以使用混合提供的API来你的情况 MouseDoubleClick 的ListBox )到的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 =htt​​p://schemas.microsoft.com/ex$p$pssion/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是从混合SDK,它不需要混合,从这里开始:
<一href=\"http://www.microsoft.com/downloads/en/details.aspx?FamilyID=f1ae9a30-4928-411d-970b-e682ab179e17&displaylang=en\" rel=\"nofollow\">http://www.microsoft.com/downloads/en/details.aspx?FamilyID=f1ae9a30-4928-411d-970b-e682ab179e17&displaylang=en

更新:我发现的东西,应该可以帮助您。检查<一href=\"http://geekswithblogs.net/lbugnion/archive/2009/11/05/mvvm-light-toolkit-v3-alpha-2-eventtocommand-behavior.aspx\"相对=nofollow>在MVVM光工具包这个链接,其中包含如何做到这一点的演练,具有的链接到需要的库。 MVVM光工具包是与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天全站免登陆