如何将列表框选定项作为按钮中的命令参数传递? [英] How to pass listbox selecteditem as command parameter in a button?

查看:16
本文介绍了如何将列表框选定项作为按钮中的命令参数传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的情况:

<ListBox ItemsSource="{Binding Path=AvailableUsers}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Id}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
<Button Command="{Binding Path=Load}" CommandParameter={???? What goes here ????}/>

我想要的是传递当前在 ListBox 中选择的 Id.我在幕后有一个视图模型,基本上看起来像这样:

What I want is to pass the Id that is currently selected in the ListBox. I have a viewmodel behind the scenes that essentially looks like this:

public class ViewModel : DependencyObject
{
    ICommand Load { get; set; }

    // dependency property but I didn't bother to write it out like one
    List<User> AvailableUsers { get; set}
}

如何使用 xaml 发送当前选定的项目?

How can I send the currently selected item using the xaml?

推荐答案

试试这个:

  1. 为您的列表框命名
  2. 将 CommandParameter 更新为:
  1. Name your listbox
  2. Update the CommandParameter to:

CommandParameter="{Binding ElementName=listBox1,Path=SelectedItem}"

CommandParameter="{Binding ElementName=listBox1,Path=SelectedItem}"

这篇关于如何将列表框选定项作为按钮中的命令参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆