这是我的情况:
<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 ????}/>
我想要的是传递当前在列表框中选择的 Id。我在幕后有一个 View 模型,基本上如下所示:
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 发送当前选定的项目?
最佳答案
试试这个:
- 为您的列表框命名
- 将 CommandParameter 更新为:
CommandParameter="{Binding ElementName=listBox1,Path=SelectedItem}"
关于wpf - 如何将列表框选定项作为按钮中的命令参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18257516/