<p>I have a ListBox that has a style defined for ListBoxItems.  Inside this style, I have some labels and a button.  One that button, I want to define a click event that can be handled on my page (or any page that uses that style).  How do I create an event handler on my WPF page to handle the event from my ListBoxItems style?</p>

<p>Here is my style (affected code only):</p>

<pre><code>&lt;Style x:Key="UsersTimeOffList"  TargetType="{x:Type ListBoxItem}"&gt;
... 
&lt;Grid&gt;
&lt;Button x:Name="btnRemove" Content="Remove" Margin="0,10,40,0" Click="btnRemove_Click" /&gt;
&lt;/Grid&gt;
&lt;/Style&gt;
</code></pre>

<p>Thanks! </p>

[{'answer_id': 28187, 'author': 'Jas', 'author_id': 777, 'author_profile': 'https://Stackoverflow.com/users/777', 'pm_score': 0, 'selected': False, 'text': '<p>You could create a user control (.ascx) to house the listbox. Then add a public event for the page.</p>\n\n<pre><code>Public Event btnRemove()\n</code></pre>\n\n<p>Then on the button click event in the usercontrol</p>\n\n<pre><code>RaiseEvent btnRemove()\n</code></pre>\n\n<p>You can also pass objects through the event just like any other method. This will allow your user control to tell your page what to delete.</p>\n'}, {'answer_id': 28252, 'author': 'Arcturus', 'author_id': 900, 'author_profile': 'https://Stackoverflow.com/users/900', 'pm_score': 4, 'selected': True, 'text': '<p>Take a look at <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.routedcommand.aspx" rel="noreferrer">RoutedCommand</a>s.</p>\n\n<p>Define your command in myclass somewhere as follows:</p>\n\n<pre><code>    public static readonly RoutedCommand Login = new RoutedCommand();\n</code></pre>\n\n<p>Now define your button with this command:</p>\n\n<pre><code>    &lt;Button Command="{x:Static myclass.Login}"  /&gt;  \n</code></pre>\n\n<p>You can use CommandParameter for extra information..</p>\n\n<p>Now last but not least, start listening to your command:</p>\n\n<p>In the constructor of the class you wish to do some nice stuff, you place:</p>\n\n<pre><code>    CommandBindings.Add(new CommandBinding(myclass.Login, ExecuteLogin));\n</code></pre>\n\n<p>or in XAML:</p>\n\n<pre><code>   &lt;UserControl.CommandBindings&gt;\n        &lt;CommandBinding Command="{x:Static myclass.Login}" Executed="ExecuteLogin" /&gt;\n   &lt;/UserControl.CommandBindings&gt;\n</code></pre>\n\n<p>And  you implement the delegate the CommandBinding needs:</p>\n\n<pre><code>    private void ExecuteLogin(object sender, ExecutedRoutedEventArgs e)\n    {\n          //Your code goes here... e has your parameter!\n    }\n</code></pre>\n\n<p>You can start listening to this command everywhere in your visual tree!</p>\n\n<p>Hope this helps</p>\n\n<p>PS You can also define the CommandBinding with a CanExecute delegate which will even disable your command if the CanExecute says so :)</p>\n\n<p>PPS Here is another example: <a href="http://www.wpfwiki.com/Default.aspx?Page=WPF%20Q13.8&amp;AspxAutoDetectCookieSupport=1" rel="noreferrer">RoutedCommands in WPF</a></p>\n'}, {'answer_id': 190051, 'author': 'Matt Hamilton', 'author_id': 615, 'author_profile': 'https://Stackoverflow.com/users/615', 'pm_score': 3, 'selected': False, 'text': '<p>As Arcturus posted, RoutedCommands are a great way to achieve this. However, if there\'s only the one button in your DataTemplate then this might be a bit simpler:</p>\n\n<p>You can actually handle any button\'s Click event from the host ListBox, like this:</p>\n\n<pre><code>&lt;ListBox Button.Click="removeButtonClick" ... /&gt;\n</code></pre>\n\n<p>Any buttons contained within the ListBox will fire that event when they\'re clicked on. From within the event handler you can use e.OriginalSource to get a reference back to the button that was clicked on.</p>\n\n<p>Obviously this is too simplistic if your ListBoxItems have more than one button, but in many cases it works just fine. </p>\n'}]
['https://Stackoverflow.com/questions/28092', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3047/']