I have a listbox whose listboxitem contains, among other things, a button, as follows:
<DataTemplate x:Key="cDataTemplate" DataType="x:Type utils:cd">
<StackPanel Orientation="Horizontal" Background="Transparent">
<Button Style="{StaticResource LIButton}" x:Name="CButton"
Command="{x:Static this:EditorCommands.RaiseCMenu}"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}">
<Image Name="Image" Source="icon_c.jpg" Width="33" Height="21"/>
<Button.ContextMenu>
<ContextMenu x:Name="ctxtCard">
<MenuItem Header="..." Command="{x:Static this:EditorCommands.abc}"/>
<MenuItem Header="..." Command="{x:Static this:EditorCommands.def}"/>
</ContextMenu>
</Button.ContextMenu>
</Button>
<StackPanel Background="Transparent">
<TextBlock HorizontalAlignment="Left">
...
</TextBlock>
<TextBlock HorizontalAlignment="Left">
...
</TextBlock>
</StackPanel>
</StackPanel>
</DataTemplate>
<Style x:Key="cListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="ContentTemplate" Value="{DynamicResource cDataTemplate}"/>
<Setter Property="Background" Value="transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border
Name="Border"
...
Background="Transparent">
<ContentPresenter Name="Content" HorizontalAlignment="Left" VerticalAlignment="Center" Opacity="0.55"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="BorderBrush" Value="Black"/>
</Trigger>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,1"/>
</Trigger>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Content" Property="Opacity" Value="1.0"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Content" Property="Opacity" Value="1.0"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
When the button in a listboxitem that is not selected is clicked, it fires its command, which raises the context menu in code-behind and selects the button's parent listboxitem. (The whole point is to raise the context menu on left-click.) But for the life of me, I can't get the button to fire the command when its parent listboxitem is already selected. Oddly, if the button inside a selected listboxitem is right-clicked, it duly raises its context menu, so the button is receiving clicks.
Thanks in advance!