Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Listbox style with a button

Tags:

c#

wpf

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?

Here is my style (affected code only):

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

Thanks!

like image 308
ScottG Avatar asked Aug 26 '08 13:08

ScottG


2 Answers

Take a look at RoutedCommands.

Define your command in myclass somewhere as follows:

    public static readonly RoutedCommand Login = new RoutedCommand();

Now define your button with this command:

    <Button Command="{x:Static myclass.Login}"  />  

You can use CommandParameter for extra information..

Now last but not least, start listening to your command:

In the constructor of the class you wish to do some nice stuff, you place:

    CommandBindings.Add(new CommandBinding(myclass.Login, ExecuteLogin));

or in XAML:

   <UserControl.CommandBindings>
        <CommandBinding Command="{x:Static myclass.Login}" Executed="ExecuteLogin" />
   </UserControl.CommandBindings>

And you implement the delegate the CommandBinding needs:

    private void ExecuteLogin(object sender, ExecutedRoutedEventArgs e)
    {
          //Your code goes here... e has your parameter!
    }

You can start listening to this command everywhere in your visual tree!

Hope this helps

PS You can also define the CommandBinding with a CanExecute delegate which will even disable your command if the CanExecute says so :)

PPS Here is another example: RoutedCommands in WPF

like image 91
Arcturus Avatar answered Oct 22 '22 03:10

Arcturus


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:

You can actually handle any button's Click event from the host ListBox, like this:

<ListBox Button.Click="removeButtonClick" ... />

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.

Obviously this is too simplistic if your ListBoxItems have more than one button, but in many cases it works just fine.

like image 6
Matt Hamilton Avatar answered Oct 22 '22 04:10

Matt Hamilton