如何在按钮中将列表框所选项目作为命令参数传递?

46

以下是我的情况:

<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。在幕后有一个视图模型,基本上看起来像这样:

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发送当前选定的项目?
1个回答

78

尝试以下操作:

  1. 给你的列表框命名
  2. 更新CommandParameter为:

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接