title: How to bind button to Listbox item
body: <p><strong>ViewModel</strong></p>
  
  <pre><code>  public class MainWindowViewModel:BindableBase
    {
       public IRelayCommand MyCommand { get; protected set; }
  
  
      private void CreateCommand()
      {
  
          this.MyCommand = new RelayCommand(MyCommandExecuted, CanExecuteMyCommand);
      }
  
      private void MyCommandExecuted(object obj)
      {
          MessageBox.Show("Command Executed");
      }
  
      private bool CanExecuteMyCommand(object obj)
      {
          return true; // The value is based on Selected Item
      }
    }
  </code></pre>
  
  <p><strong>XAML</strong></p>
  
  <pre><code>     &lt;ListBox
               x:Name="myListBox" 
               ItemsSource="{Binding Path=MyClass}"
               &lt;ListBox.ItemTemplate&gt;
          &lt;DataTemplate&gt;
                  &lt;Expander Header="{Binding Path=HeaderName}" IsExpanded="True"&gt;
                      &lt;StackPanel&gt;
                          &lt;DataGrid
                               x:Name="dataGrid"
  
                               AutoGenerateColumns="False"
                               ItemsSource="{Binding Path=RowVal}"  SelectedItem="{Binding CurrentItem}"/&gt;
                      &lt;/StackPanel&gt;
                  &lt;/Expander&gt;
          &lt;/DataTemplate&gt;
          &lt;/ListBox.ItemTemplate&gt;
  
  &lt;Button Content="Select" 
  
              Command="{Binding Path=MyCommand }" 
              CommandParameter="{Binding ElementName=myListBox,Path=SelectedItem}"/&gt;
  </code></pre>
  
  <p><strong>DataClass</strong></p>
  
  <pre><code>  public class DataClass 
     {
      public string HeaderName { get; set; }
      public object RowVal { get; set; }
      public ObservableCollection&lt;DataGridColumn&gt; ColumnCollection { get; set;}
      private object currentItem;
  
      public object CurrentItem
      {
          get
          {
              return currentItem;
          }
          set
          {
              currentItem = value;
  
          }
      }
  
  }
  </code></pre>
  
  <p>How can I bind my button to Listbox item which is CurrentItem in DataClass ?</p>