Search code examples
c#wpflistviewlistviewitemselectionchanged

ListView with RadioButtons fires/raises SelectionChanged ONLY on Right-Click (Need left-click)


I have the following XAML/Control Template for a ListViewItem:

<Window.Resources>
    <ControlTemplate x:Key="ItemTemplate" TargetType="ListViewItem">
        <Border BorderThickness="{TemplateBinding Border.BorderThickness}" Padding="0,5,0,5" BorderBrush="{TemplateBinding Border.BorderBrush}" 
                Background="{TemplateBinding Panel.Background}" SnapsToDevicePixels="True">
            <ContentPresenter Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                              HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" 
                              SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
        </Border>
    </ControlTemplate>

    <Style TargetType="ListViewItem">
        <Setter Property="Template" Value="{StaticResource ItemTemplate}" />
    </Style>

    <DataTemplate x:Key="ItemDataTemplate">
        <RadioButton
        x:Name="radiobutton" GroupName="LeGroup"
        Content="{Binding Path=Name}" Checked="radiobutton_Checked" />
    </DataTemplate>
</Window.Resources>

AND when I try to set the SelectionChanged to fire, it ONLY fires when a radio button is checked with a Mouse RIGHT Click. I need it to fire when it is a NORMAL Left Click.

<ListView x:Name="radioListView" SelectionMode="Single" ItemsSource="{Binding}" ItemTemplate="{StaticResource ItemDataTemplate}" SelectionChanged="radioListView_SelectionChanged" Loaded="radioListView_Loaded"></ListView>

Can't seem to find anything referring to forcing a selectionchanged event on a right click. Standard is a left click.

Thank you in advance :)

EDIT:

So I noticed that the SelectionChanged event fires when I Click OUTSIDE of the radio button (and its associated text) on Left Click. It's as if the radio button is working independently from the listview. The SelectionChanged event fires on a Right Click INSIDE the radio button element. Weird, still trying to figure it out.


Solution

  • I'm not sure why the Left mouse button doesn't work, but here is the style I use for displaying a ListBox with RadioButtons. It works fine with both Right and Left mouse clicks

    <Style x:Key="ListBoxAsRadioButtonsStyle" TargetType="{x:Type ListBox}">
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Cycle" />
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="{x:Type ListBoxItem}" >
                    <Setter Property="Margin" Value="2, 2, 2, 0" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Border Background="Transparent">
                                    <RadioButton IsHitTestVisible="False" Focusable="false" 
                                                 Content="{TemplateBinding ContentPresenter.Content}"  
                                                 IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Setter.Value>
        </Setter>
    </Style>
    

    It is used like

    <ListBox ItemsSource="{Binding MyItems}" 
             Style="{StaticResource ListBoxAsRadioButtonsStyle}" />
    

    If I had to guess at your problem, I would guess that the regular ListView Selection behavior is marking the LeftMouseClick event as Handled when you click on an item