2010-11-06 85 views
0

我的数据绑定了我的MVVM Light Windows Phone 7应用程序中的列表框,并且当用户单击我的项目时我想要在我的视图模型中调用命令列表框。Mvvm Light ListBox MouseButtonLeftDown和EventToCommand - 如何传递点击的项目

我该使用EventToCommand行为,一切都很好,但我无法通过与被点击,如果我使用MouseLeftButtonDown事件列表元素相关联的数据项做...

如果我使用SelectionChanged事件,那么我可以将行为的CommandParameter绑定到ListBox的SelectedItem,但我真的想使用MouseLeftButtonDown事件。

任何想法?我不想通过设置“PassEventArgsToCommand”选项来污染我的View Model,并且在任何情况下我都不确定我是否可以从MouseButtonEventArgs中获取选定的数据项。

现在我正在设置代码隐藏中的事件处理程序,并从那里调用ViewModel,使用“sender”来获取数据项。

感谢,

Damian 

回答

0

它看起来像这个问题实际上并没有什么意义 - 在列表框被解雇MouseLeftButtonDown事件不与ListBox中的特定项目相关联。

相反,我正在寻找与我已经与ListBox关联的ItemTemplate上的这个事件。

0

杰西自由在这里给出了一个很好的例子:Passing Parameters...

但是,如果你正在使用的Windows Phone 7.5,你必须改变一两件事,这是你不能使用Galasoft EventToCommand语法了,如下所示在这个例子中:

<i:Interaction.Triggers> 
<i:EventTrigger EventName="SelectionChanged"> 
    <!--<GalaSoft_MvvmLight_Command:EventToCommand x:Name="SelectionCommand" Command="{Binding SwitchProfileCommand, Mode=OneWay}" CommandParameter="{Binding SelectedItem, ElementName=lboxProfiles}"/>--> 

    <i:InvokeCommandAction Command="{Binding SwitchProfileCommand, Mode=OneWay}" CommandParameter="{Binding SelectedItem, ElementName=lboxProfiles}" /> 

</i:EventTrigger> 

同样适用于Windows Phone 7.5,检查出http://windowsphonegeek.com/articles/ListBox-ContextMenu-with-MVVM-in-Windows-Phone使用与连接到每个项目上下文菜单中的命令。

相关问题