[英]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: 基本上你有几个选择:
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实现的
RelayCommand
和CommandManager
类。 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. 将对象绑定到
ListBox
的SelectedValue
并监听要更改的值可能更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.