Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7496969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:52:29+00:00 2026-05-29T18:52:29+00:00

I am trying to build a WPF application (using C#.net) in which I want

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T18:52:32+00:00Added an answer on May 29, 2026 at 6:52 pm

    Nilesh,

    you should be using binding the button to a command.
    For example if your data item is defined like this:

    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 this will invoke the ClickMe method.

    <DockPanel>
        <Button Content="Click Me" Command="{Binding ClickMeCommand}" />
    </DockPanel>
    

    Or you can put the command in the parent view model:

    public class MainViewModel : ViewModelBase
    {
        public IEnumerable<MyItem> Items { get; private set; }
    
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            Items = new List<MyItem>
                        {
                            new MyItem{ ISBN = "ISBN", BookName = "Book", PublisherName = "Publisher"}
                        };
            ClickMeCommand = new RelayCommand<MyItem>(ClickMe);
        }
    
        private void ClickMe(MyItem item)
        {
            Debug.WriteLine(string.Format("This book was clicked: {0}", item.BookName));
        }
    
        public ICommand ClickMeCommand { get; set; }
    

    }

    and bind to that

    <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 is using MVVM light and I’m assuming you have

      <ListBox ItemTemplate="{StaticResource MyTemplate}" ItemsSource="{Binding Items}"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF, ClickOnce application that I am trying to build using the
I am trying to build a WPF application using the MVVM pattern. It would
Im trying to build call to action button on my site using jQuery. I
I'm trying to write validation rules for my data objects in a WPF application
I am trying to build a raster drawing application using C# and so far
I'm trying to host an XNA game inside a WPF window using the Windows
I have a wpf Application in which I am trying to reference a class
I am trying to create a parent WPF/surface application which will host multiple WPF/surface
I'm trying to build a validation rule for a C# WPF application by creating
I'm trying to build a template for a treeView in WPF and c#. My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.