Nilesh
you must use button binding to the command. For example, if your data item is defined as follows:
public class MyItem : ViewModelBase { public MyItem() { ClickMeCommand = new RelayCommand(ClickMe); } private void ClickMe() { Debug.WriteLine("I was clicked"); } public string ISBN { get; set; } public string BookName { get; set; } public string PublisherName { get; set; } public ICommand ClickMeCommand { get; set; } }
Then it will call the ClickMe method.
<DockPanel> <Button Content="Click Me" Command="{Binding ClickMeCommand}" /> </DockPanel>
Or you can put this command in the parent view model:
public class MainViewModel : ViewModelBase { public IEnumerable<MyItem> Items { get; private set; }
}
and snap to it
<DockPanel> <Button Content="Click Me" CommandParameter="{Binding}" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBox}}, Path=DataContext.ClickMeCommand}" /> </DockPanel>
Note that the code above uses the MVVM indicator, and I assume that you have
<ListBox ItemTemplate="{StaticResource MyTemplate}" ItemsSource="{Binding Items}"/>
source share