16

I have a DataTemplate that represents AppBar buttons that I declare through a collection of custom AppBarCommand objects.

 public AppBarCommand(RelayCommand command, string buttonstyle) { Command = command; ButtonStyle = buttonstyle; } <DataTemplate> <Button Command="{Binding Command}" Style="{Binding ButtonStyle, Converter={StaticResource StringNameToStyleConverter}}"/> </DataTemplate> 

I would like to add a CommandParameter binding, but the parameter has to be the Button itself. This is so I can set the PlacementTarget of a Callisto flyout. Is this possible?

1
  • Probably easier to handle the Button's Click event. The first argument you receive in your handler will be the Button. Commented Sep 13, 2012 at 20:09

3 Answers 3

53
<Button Command="{Binding Command}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}" /> 

Your Command property should be the generic version of the RelayCommand: RelayCommand<object> for instance.

Sign up to request clarification or add additional context in comments.

Comments

6

Answer like Miklós Balogh said, or you can:

<Button x:Name="MyButton" Command="{Binding Command}" CommandParameter={Binding ElementName=MyButton ... /> 

Comments

1

I had the same problem but I used it in a bit different context:

<MenuItem ItemsSource="{Binding MyList}"> <MenuItem.ItemContainerStyle> <Style TargetType="{x:Type MenuItem}"> <Setter Property="Command" Value="{Binding RelativeSource={ RelativeSource FindAncestor, AncestorType={ x:Type Window}}, Path= DataContext.MyListItemCommand}"/> <Setter Property="CommandParameter" Value="{Binding}" /> </Style> </MenuItem.ItemContainerStyle> </MenuItem> 

so I assume that even if you write it like this

<Button Command="{Binding Command}" CommandParameter="{Binding}" /> 

it should work.

I also recommend reading this post to understand it better.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.