NTinkicht NTinkicht - 2 months ago 11
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:

LisViewItem

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" >
<ListView.LayoutTransform>
<RotateTransform Angle="{Binding IsVertical, Converter={StaticResource AngleOfBool}}"></RotateTransform>
</ListView.LayoutTransform>
<ListView.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch" Background="Transparent" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<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}}" />
</Grid>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

Answer

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">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto"></ColumnDefinition>
        <ColumnDefinition Width="auto"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <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">
        <Grid.RowDefinitions>
            <RowDefinition Height="0.5*"></RowDefinition>
            <RowDefinition Height="0.5*"></RowDefinition>
        </Grid.RowDefinitions>
        <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>
    </Grid>
</Grid>

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

UPDATE >>>

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
    <Style.Resources>
        <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" />
    </Style.Resources>
</Style>

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}" ... />
Comments