简体   繁体   English

如何在MVVM ListBox Silverlight中实现SelectionChanged

[英]How can I realize SelectionChanged in MVVM ListBox Silverlight

The ListBox control does not implement a Command property. ListBox控件未实现Command属性。 I have to attach some functionality to the SelectionChanged event. 我必须为SelectionChanged事件附加一些功能。 Somebody knows how can I do it? 有人知道我该怎么办? Please help me 请帮我

I prefer using a binding to the SelectedItem and implementing any functionality in the setting of the binding property. 我更喜欢使用绑定到SelectedItem并在绑定属性的设置中实现任何功能。

<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.
        }
    }
}

This is the way where You can Reach the Selection changed events in Your MVVM Application First Of all i tell you that Command Property only work in Button now we have to Explicitly binding that property in our Selection Changed event like List box or combo box in Your XMAL file 这是你可以在你的MVVM应用程序中达到选择更改事件的方式首先我告诉你Command属性只适用于Button现在我们必须在我们的选择更改事件中显式绑定该属性,如列表框或你的组合框XMAL文件

<ListBox Name="MyListBox" ItemsSource="{Binding ListItems}" Height="150" Width="150" Margin="281,32,-31,118">

        <Local:Interaction.Triggers>
            <Local:EventTrigger EventName="SelectionChanged">
                <Local:InvokeCommandAction Command="{Binding MyCommand}" CommandParameter="{Binding ElementName=MyListBox,Path=SelectedItem}"/>
            </Local:EventTrigger>
        </Local:Interaction.Triggers>
    </ListBox>

for this you have to add dll Syatem.Windows.Interactivity now u have to add references in your xaml file namespace like 为此你必须添加dll Syatem.Windows.Interactivity现在你必须在你的xaml文件名称空间中添加引用

 xmlns:Local="clr-namespace:System.Windows.Interactivityassembly=System.Windows.Interactivity"

in your ViewModel Class you have to define your Command in Con structure 在ViewModel类中,您必须在Con结构中定义Command

 public ViewModel123()
    {
         MyCommand = new RelayCommand<string>(TestMethod);

    }

now create the TestMethod method which can handle the selection changed event 现在创建TestMethod方法,该方法可以处理选择更改事件

 private void TestMethod(string parameter)
    {
        MessageBox.Show(parameter);
    }

i hope this may help u. 我希望这可以帮助你。

Basically you have a few options: 基本上你有几个选择:

  1. Use the property SelectedItem of ListBox to bind to a property in the backend (ie in view model) and perform logic in the setter as described by Cameron MacFarland . 使用ListBox的SelectedItem属性绑定到后端的属性(即在视图模型中),并按照Cameron MacFarland的描述在setter中执行逻辑。
  2. Use a third party library that has a generic event to command behavior like in the link posted by Pedro Lamas . 使用具有通用事件的第三方库来命令行为,如Pedro Lamas发布的链接。
  3. If you don't want to use third party libraries or writing logic inside property setter is somehow unacceptable you can create your own behavior for ListBox control. 如果您不想使用第三方库或在属性内部编写逻辑,则设置器在某种程度上是不可接受的,您可以为ListBox控件创建自己的行为。 It would subscribe to control's SelectionChanged event and execute a command (the command could be a dependency property exposed by the behavior). 它将订阅控件的SelectionChanged事件并执行命令(该命令可以是行为公开的依赖属性)。

Think this post from Laurent Bugnion will help you solve the problem: 想想Laurent Bugnion的这篇文章将帮助您解决问题:

http://geekswithblogs.net/lbugnion/archive/2010/05/19/handling-datagrid.selecteditems-in-an-mvvm-friendly-manner.aspx http://geekswithblogs.net/lbugnion/archive/2010/05/19/handling-datagrid.selecteditems-in-an-mvvm-friendly-manner.aspx

The post above mentions the DataGrid but I do think it will work with the ListBox too! 上面的帖子提到了DataGrid,但我认为它也适用于ListBox!

Best regards and Happy New Year!! 最好的问候和新年快乐! :) :)

I would suggest using RelayCommand . 我建议使用RelayCommand Either use the MVVM Light Toolkit or just use the RelayCommand and CommandManager classes from Josh Smith's implementations . 要么使用MVVM Light Toolkit,要么只使用Josh Smith实现RelayCommandCommandManager类。 I personally use just the two classes, so I don't need the entire toolkit. 我个人只使用这两个类,所以我不需要整个工具包。

While this will definitely work, there might be an easier way depending on what you are doing. 虽然这肯定会奏效,但根据您的工作情况,可能会有更简单的方法。 It might just be easier to bind an object to the SelectedValue of the ListBox and listen for that value to change. 将对象绑定到ListBoxSelectedValue并监听要更改的值可能更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用ListBox的SelectionChanged()事件时,如何为TextBlock设置动画? - How can I animate a TextBlock when a SelectionChanged() event of a ListBox is called? 如何使用MVVM绑定Silverlight 4中的ComboBox的SelectionChanged事件? - How to bind a SelectionChanged event of ComboBox in silverlight 4 using MVVM? Silverlight MVVM绑定ListBox - Silverlight MVVM binding a ListBox Silverlight + MVVM-Light中的SelectionChanged事件绑定 - SelectionChanged event binding in Silverlight+MVVM-Light 如何在带有导航服务的Silverlight中实现MVVM? - How can I implement MVVM in Silverlight with Navigation service? 将 Silverlight 4 与 MVVM 一起使用,如何以编程方式滚动到页面顶部 - Using Silverlight 4 with MVVM, how can I to scroll to the top of a page programatically 使用MVVM的Silverlight ListBox分组 - Silverlight ListBox Grouping using MVVM 在这个 Silverlight MVVM 示例中,如何将 ModelView 连接到 Model? - How can I connect the ModelView to the Model in this Silverlight MVVM example? 如何在Silverlight中将按钮的单击事件和列表框的selecteditem更改绑定到mvvm中的视图模型 - how to bind the click event of a button and the selecteditemchanged of a listbox to a viewmodel in mvvm in Silverlight 如何使用MVVM silverlight使用Button将列表删除到列表框 - How remove list to listbox with Button using MVVM silverlight
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM