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 502447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:17:32+00:00 2026-05-13T06:17:32+00:00

I have added a button to a template for a ListBoxItem. I want to

  • 0

I have added a button to a template for a ListBoxItem. I want to know how to catch the click for the button (not selecting the ListboxItem).

The button clicks just fine in the UI, but I can’t seem to find out how to wire up code so I can catch it.

I tried using:

<EventSetter Event="Click" Handler="Button_Click"></EventSetter>

but that seemed to want to be on the ListBoxItem. I could not see a way to wire it up to the button.

Here is the style I am working with incase it helps:

<Window x:Class="WIAssistant.Main"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="My App" Height="587" Width="1246" Name="MainForm" FontSize="14">

<Window.Resources>
  <Style x:Key="CheckBoxListStyle" TargetType="ListBox">
     <Style.Resources>
        <Style TargetType="ListBoxItem">
          <Setter Property="Template">
             <Setter.Value>
                 <ControlTemplate TargetType="ListBoxItem">
                   <Grid ScrollViewer.CanContentScroll="True" Margin="2">
                      <Grid.ColumnDefinitions>
                      <ColumnDefinition Width="Auto" />
                      <ColumnDefinition Width="Auto" />
                      <ColumnDefinition Width="Auto" />
                      <ColumnDefinition />
                   </Grid.ColumnDefinitions>
                   <CheckBox VerticalAlignment="Center" IsChecked="{Binding IsSelected,
                                                 RelativeSource={RelativeSource TemplatedParent},
                                                 Mode=TwoWay}" />
                   <TextBlock VerticalAlignment="Center" Grid.Column="1" Margin="5,0,5,0" Text="{Binding Id}" />
                   <TextBlock VerticalAlignment="Center" Grid.Column="2" Margin="5,0,5,0" Text="{Binding Title}" />
                   <Button HorizontalAlignment="Right" Grid.Column="3" Margin="5,0,5,0" Width="25">-&gt;</Button>
                 </Grid>
               </ControlTemplate>
             </Setter.Value>
          </Setter>
        </Style>
      </Style.Resources>
      <Setter Property="ItemsPanel">
        <Setter.Value>
           <ItemsPanelTemplate>
             <WrapPanel Orientation="Vertical"  />
           </ItemsPanelTemplate>
         </Setter.Value>
      </Setter>
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Background" Value="Transparent" />
  </Style>
</Window.Resources>

... (In a grid in later code)

 <ListBox SelectionMode="Multiple" Style="{StaticResource CheckBoxListStyle}"
          Name="lstQueryResults" SelectionChanged="lstQueryResults_SelectionChanged">
 </ListBox>

Any way to capture the click for the button would be appreciated. (Note, just adding a click event to the button gives an error saying to use an EventSetter.

  • 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-13T06:17:33+00:00Added an answer on May 13, 2026 at 6:17 am

    The issue for the event handler seems to be the style-inside-style. Too keep the style in the window resources, try the following:

    <Window.Resources>
        <DataTemplate x:Key="ListBoxItemTemplate">
            <Grid ScrollViewer.CanContentScroll="True" Margin="2">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <CheckBox VerticalAlignment="Center" IsChecked="{Binding IsSelected, Mode=TwoWay}" />
                <TextBlock VerticalAlignment="Center" Grid.Column="1" Margin="5,0,5,0" Text="{Binding Id}" />
                <TextBlock VerticalAlignment="Center" Grid.Column="2" Margin="5,0,5,0" Text="{Binding Title}" />
                <Button HorizontalAlignment="Right" Grid.Column="3" Margin="5,0,5,0" Width="25" Click="Button_Click">-&gt;</Button>
            </Grid>
        </DataTemplate>
    </Window.Resources>
    

    +

    <ListBox ItemTemplate="{StaticResource ListBoxItemTemplate}" />
    

    EventSetter in the template also works, but apparently the design view in VS stops working.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page with some dynamically added buttons. If you click a button
I have a Repeater with a Button inside the ItemTemplate . I added the
I have added some code which compiles cleanly and have just received this Windows
I have added the required assemblies and registered the NVelocityViewFactory in global.asax.cs page but
If I have added/removed/modified a large number of files in my local ClearCase view,
I have a gridview that I have added a checkbox column using a templatefield
EDIT: I have added Slug column to address performance issues on specific record selection.
I have a program with two TForm classes and have added a TMainMenu to
I have an existing database with the table Transactions in it. I have added
I have written a control in C# that derives from System.Windows.Forms.Control. I have added

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.