如何通过列表框将selectedItem作为一个按钮命令参数? [英] How to pass listbox selecteditem as command parameter in a button?

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

问题描述

下面是我的情况:

 < ListBox中的ItemsSource ={绑定路径= AvailableUsers}>
    < ListBox.ItemTemplate>
        <&DataTemplate的GT;
            < TextBlock的文本={绑定路径= ID}/>
        < / DataTemplate中>
    < /ListBox.ItemTemplate>
< /列表框>
<按钮命令={绑定路径=负载}CommandParameter = {????善有善报这里????} />

我要的是通过当前在列表框中选择的ID。我基本上是这样的幕后视图模型:

公共类视图模型:DependencyObject的
{
    ICommand的负载{搞定;组; }    //依赖属性,但我没有刻意去写出来诸如One
    清单<使用者> AvailableUsers {搞定;组}
}

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


解决方案

试试这个:


  1. 命名您的列表框

  2. 更新CommandParameter为:


      

    CommandParameter ={绑定的ElementName = listBox1中,路径=的SelectedItem}


  3.   


Here is my situation:

<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 ????}/>

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}
}

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

解决方案

Try this:

  1. Name your listbox
  2. Update the CommandParameter to:

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

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

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