46

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?

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Price Jones
  • 1,948
  • 1
  • 24
  • 40

1 Answers1

77

Try this:

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

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

user1069816
  • 2,763
  • 2
  • 26
  • 43
mohammad jannesary
  • 1,811
  • 1
  • 26
  • 27