Question

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.

Was it helpful?

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

OTHER TIPS

Well thats strange! Selection takes place on Right click???

All I can suggest you is to take an eventless approach ...

You could use the MVVM pattern by binding SelectedItem / SelectedValue properties etc.

Also you could harness single selection mode of ListView to automatically "group" radio buttons so that ONLY one is selected at time

e.g. radiobutton's IsChecked bound to ancestor ListViewItem's IsSelected property, two way. This way when a radio button is checked it would automatically select that list view row and SelectedItem\Value binding would fire automatically. And being in single selection mode of ListView, next radio button click would automatically de-select previous row.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top