I am trying to build a WPF application (using C#.net) in which I want to add button inside ListBox.
Here is the data template which I have placed in the resource dictionary
<DataTemplate x:Key="MYTemplate">
<StackPanel Margin="4">
<DockPanel>
<TextBlock Text="ISBN No:" DockPanel.Dock="Left" Margin="5,0,10,0" Foreground="AliceBlue" />
<TextBlock Text=" " />
<TextBlock Text="{Binding ISBN}" Foreground="LimeGreen" FontWeight="Bold" />
</DockPanel>
<DockPanel>
<TextBlock Text="Book Name:" DockPanel.Dock="Left" Margin="5,0,10,0" Foreground="AliceBlue"/>
<TextBlock Text=" " />
<TextBlock Text="{Binding BookName}" Foreground="LimeGreen" FontWeight="Bold" />
</DockPanel >
<DockPanel >
<TextBlock Text="Publisher Name:" DockPanel.Dock="Left" Margin="5,0,10,0" Foreground="AliceBlue" />
<TextBlock Text=" " />
<TextBlock Text="{Binding PublisherName}" Foreground="LimeGreen" FontWeight="Bold" />
</DockPanel>
<DockPanel>
<Button Name="MyButton" Content="Click Me">
</Button>
</DockPanel>
</StackPanel>
</DataTemplate>
How do add the click event to the button tag in the above template?
Also where should I place the method which will be called when button is clicked.
Nilesh,
you should be using binding the button to a command.
For example if your data item is defined like this:
Then this will invoke the ClickMe method.
Or you can put the command in the parent view model:
}
and bind to that
Note that the code above is using MVVM light and I’m assuming you have