gpt4 book ai didi

wpf - 如何将列表框选定项作为按钮中的命令参数传递?

转载 作者:行者123 更新时间:2023-12-02 18:09:37 25 4
gpt4 key购买 nike

这是我的情况:

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

最佳答案

试试这个:

  1. 为您的列表框命名
  2. 将 CommandParameter 更新为:

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

关于wpf - 如何将列表框选定项作为按钮中的命令参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18257516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com