将所选项目作为参数发送到视图模型中的方法[WPF,Caliburn]

2 wpf listbox caliburn.micro

我有这个问题.我在WPF中使用了caliburn micro.在视图中我有listbox,并在视图模型中绑定事件MouseDoubleClick方法.我想发送参数选择列表框项目.但我不知道怎么做.

在视图我有这个:

    <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"/>
Run Code Online (Sandbox Code Playgroud)

在视图模型中我有这个方法:

    public void SendRp(string key)
    {
        MessageBox.Show(key);
    }
Run Code Online (Sandbox Code Playgroud)

任何进展,谢谢.

thu*_*eys 6

我不太了解校准,但我猜是你必须写

Micro:Message.Attach="[MouseDoubleClick]=[Action SendRp(Friends.SelectedItem)]"
Run Code Online (Sandbox Code Playgroud)

你也应该省略SelectedItem="Key"或使用你的ViewModel绑定,如下所示:

SelectedItem="{Binding Key}"
Run Code Online (Sandbox Code Playgroud)

如果由于我缺乏校准知识,我的帖子完全是假的,请提前抱歉

  • 您可以使用 `SendRp($this)`,因为 CM 将 `SelectedItem` 设置为 `ListBox` 的 `$this`。 (2认同)