send selected item as parameter to method in view-model [WPF,Caliburn]

不问归期 提交于 2019-12-12 18:22:41

问题


I have this problem. I use caliburn micro in WPF. In view I have listbox, and I bind on event MouseDoubleClick method in view-model. I would like send as parameter selected listbox item. But I don’t know how do it.

in view I have this:

    <ListBox Name="Friends" 
             SelectedItem="Key"
             Style="{DynamicResource friendsListStyle}"
             Grid.Row="2" 
             Margin="4,4,4,4"

             Micro:Message.Attach="[MouseDoubleClick]=[Action SendRp(Key)]"

             PreviewMouseRightButtonUp="ListBox_PreviewMouseRightButtonUp"
             PreviewMouseRightButtonDown="ListBox_PreviewMouseRightButtonDown" 
             MouseRightButtonDown="FriendsListBoxMouseRightButtonDown"/>

In view model I have this method:

    public void SendRp(string key)
    {
        MessageBox.Show(key);
    }

Any advance, thank.


回答1:


I dont know much about caliburn but my guess is you have to write

Micro:Message.Attach="[MouseDoubleClick]=[Action SendRp(Friends.SelectedItem)]"

also you should either omit the SelectedItem="Key" or use a binding to your ViewModel like this:

SelectedItem="{Binding Key}"

sorry in advance if my post is completely bogus due to my lack of caliburn knowledge



来源:https://stackoverflow.com/questions/4450042/send-selected-item-as-parameter-to-method-in-view-model-wpf-caliburn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!