2010-11-06 48 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使用與連接到每個項目上下文菜單中的命令。

相關問題