NTinkicht NTinkicht - 11 months ago 52
C# Question

Disable Control's Selection Background Effect in WPF

I have a ListView which shows a collection of objects. I want to disable the "Blue effect" or "Blue rectangle" when clicking on the ListViewItem, and Let only the object in this Item enabled. Does anyone has an idea about that ??

Here is an Example of one of my ListViewItems:


Here is my listView:

<ListView SelectionMode="Single" IsManipulationEnabled="True" HorizontalAlignment="Left" HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch" ScrollViewer.VerticalScrollBarVisibility="Auto" ItemsSource="{Binding ChildrenList}" Background="Transparent" BorderBrush="Transparent" >
<RotateTransform Angle="{Binding IsVertical, Converter={StaticResource AngleOfBool}}"></RotateTransform>
<Grid HorizontalAlignment="Stretch" Background="Transparent" >
<ColumnDefinition Width="Auto"></ColumnDefinition>
<CheckBox Content="{Binding Id}" Height="auto" Name="Flag" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Column="0" IsChecked="{Binding IsVisible}"/>
<Grid Grid.Column="1">
<area:Area HorizontalAlignment="Stretch" Visibility="{Binding IsVisible, Converter={StaticResource VisibilityOfBool}}" />

Answer Source

Setting the IsEnabled property of a parent control to False will automatically set the IsEnabled property of every child control to False as well. The only way around this is to set the IsEnabled property of the individual controls that you want to be disabled to False, instead of that of the parent control.

However, you can create one bool property to Bind to the IsEnabled property of all of the relevant controls so that at least, you can disable and enable them all using the one property:

<Grid Name="Parent">
        <ColumnDefinition Width="auto"></ColumnDefinition>
        <ColumnDefinition Width="auto"></ColumnDefinition>
    <ContentControl IsEnabled="AreInnerControlsEnabled" Grid.Column="0" Content="{Binding}" Visibility="{Binding IsVisible, Converter= {StaticResource VisibilityOfBool} }"></ContentControl>
    <Grid Grid.Column="1" Visibility="{Binding IsVisible, Converter= {StaticResource VisibilityOfBool} }" HorizontalAlignment="Stretch">
            <RowDefinition Height="0.5*"></RowDefinition>
            <RowDefinition Height="0.5*"></RowDefinition>
        <Label IsEnabled="AreInnerControlsEnabled" Grid.Row="0" Background="Transparent" Content="{Binding Top}" VerticalAlignment="Top" HorizontalAlignment="Stretch"></Label>
        <Label Grid.Row="1" Background="Transparent" Content="{Binding Bottom}" VerticalAlignment="Bottom" HorizontalAlignment="Stretch"></Label>

Using this method, only the controls using the AreInnerControlsEnabled property will be affected.


So it turns out that you actually want to remove the default selection colour of the ListView... that is very different to what you 'appeared' to be asking. However, now I know that, you can achieve that very easily by using a simple Style:

<Style x:Key="HiddenDefaultSelectionStyle" TargetType="{x:Type ListViewItem
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />

You only need one of these to hide that colour, but I have shown you the rest for future reference. You can use it like this:

<ListView ItemContainerStyle="{StaticResource HiddenDefaultSelectionStyle}" ... />