如何在 MVVM ListBox Silverlight 中实现 SelectionChanged [英] How can I realize SelectionChanged in MVVM ListBox Silverlight

查看:29
本文介绍了如何在 MVVM ListBox Silverlight 中实现 SelectionChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ListBox 控件没有实现 Command 属性.我必须将一些功能附加到 SelectionChanged 事件.有人知道我该怎么做?请帮帮我

The ListBox control does not implement a Command property. I have to attach some functionality to the SelectionChanged event. Somebody knows how can I do it? Please help me

推荐答案

我更喜欢使用 SelectedItem 的绑定并在绑定属性的设置中实现任何功能.

I prefer using a binding to the SelectedItem and implementing any functionality in the setting of the binding property.

<ListBox ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" />

...

public class ViewModel
{
    public IEnumerable<Item> Items { get; set; } 

    private Item selectedItem;
    public Item SelectedItem
    {
        get { return selectedItem; }
        set
        {
            if (selectedItem == value)
                return;
            selectedItem = value;
            // Do logic on selection change.
        }
    }
}

这篇关于如何在 MVVM ListBox Silverlight 中实现 SelectionChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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