Search code examples
c#wpfradio-buttonstylinglistviewitem

Removing the 'selection' from a radio button in a listview control


This feels like the solution would be obvious, but apparently not.

I've got a listview on my window defined like so:

<ListView SelectedValue="{Binding Gender, ValidatesOnDataErrors=True}" Grid.Column="1" Grid.Row="3" SelectedValuePath="Tag" BorderThickness="0" Margin="2" SelectionMode="Single">
    <RadioButton GroupName="Gender" Margin="2" Tag="M" Content="Male" IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem}, Path=IsSelected}"/>
    <RadioButton GroupName="Gender" Margin="2" Tag="F" Content="Female" IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem}, Path=IsSelected}"/>
</ListView>

When one of those radio buttons is selected, it has a grey background and border around the item. I want to remove/hide the background and border. How do I do this?

I've tried setting Focusable to false. Various styles, templates, triggers etc. but these have no effect. I tried all solutions from this question but they didn't work.

How else can I remove this styling on the list view items. Basically I want the grey background and border gone when the control doesn't have focus.

EDIT: Adding a picture to illustrate what I'm trying to achieve. I want the border and background around the 'Male' radiobutton removed/hidden. So this is what I have:

Part of my form

And this is what I want: Expected

This is what I think should work (according to answers so far, but does nothing. The style just stays the same.)

<!-- This doesn't change the style... -->
<ListView.Resources>
    <Style TargetType="ListViewItem">
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="BorderBrush" Value="Transparent"/>
                <Setter Property="Background" Value="Transparent"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</ListView.Resources>

<!-- Neither does this... -->
<Style TargetType="{x:Type ListViewItem}">
    <Style.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsSelected" Value="True"/>
                <Condition Property="IsFocused" Value="False"/>
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
                <Setter Property="Foreground" Value="{x:Null}"/>
                <Setter Property="Background" Value="{x:Null}"/>
                <Setter Property="BorderBrush" Value="{x:Null}"/>                          
            </MultiTrigger.Setters>
        </MultiTrigger>
    </Style.Triggers>
</Style>

Solution

  • Ok, I have no idea what I've done, but I found the answer my tweaking the XAML in this blog post:

    I've ended up with this Style, I have no idea what it does, but it does what I want it to do.

    The full XAML for my ListView now looks like this, I'll move the style into a resource dictionary later.

    <ListView SelectedValue="{Binding Gender, ValidatesOnDataErrors=True}" Grid.Column="1" Grid.Row="3" SelectedValuePath="Tag" BorderThickness="0" Margin="2" SelectionMode="Single">
        <ListView.Resources>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <Border x:Name="Bd"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    Background="{TemplateBinding Background}"
                                    Padding="{TemplateBinding Padding}"
                                    SnapsToDevicePixels="true">
                                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                            </Border>
                            <ControlTemplate.Triggers>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="Selector.IsSelectionActive"
                            Value="False" />
                                        <Condition Property="IsSelected"
                            Value="True" />
                                    </MultiTrigger.Conditions>
                                    <Setter Property="Background"
                    TargetName="Bd"
                    Value="Transparent" />
                                </MultiTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListView.Resources>
        <RadioButton GroupName="Gender" Margin="2" Tag="M" Content="Male" IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem}, Path=IsSelected}"/>
        <RadioButton GroupName="Gender" Margin="2" Tag="F" Content="Female" IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem}, Path=IsSelected}"/>
    </ListView>