I have a control that inherits from Panel. This control creates button controls based on a collection bound to the custom control. How to I handle the button.click event from the children? Here is my XAML. The command does not get executed.
<ScrollViewer HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Height="Auto" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<control:TreeContainer HorizontalAlignment="Center"
Width="Auto" Height="Auto" Margin="8,8,8,8" VerticalBuffer="20"
RootNode="{Binding Path=RootNode}"
ChildrenNodes="{Binding Path=ChildrenNodes}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Button.Click">
<cmd:EventToCommand Command="{Binding Path=TreeContainerClickCommand, Mode=OneWay}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</control:TreeContainer>
</ScrollViewer>
you can set a Style to all Buttons in the control, and bind the command there. When you add the Style with
TargetType="Button"
to the TreeContainer, it will be applied to all Buttons in the TreeContainer.if do not have access to the TreeContainer ViewModel in your Buttons, you'll have to access it via RelativeSource:
note that you'll have to change the Path to
DataContext.*
because your Source isTreeContainer
, not its ViewModel.edit: if you really need an
EventTrigger
to get yourEventArgs
, you could do the same like above with the Template of the Button. Then you can define a new Template in which you can use your interaction-Triggers.