CaptainLama
CaptainLama

Reputation: 85

Disable the FocusStyle of the ListViewItem Element in a ListView in c#

I'am generating a ListView at runtime and populate it continuously. The problem is that I want disable the color change when hovering over an item or selecting it.

Every ListViewItem is not focusable. This way the color change doesn't stay and is only shown when hovering over and item. I tried disable this by making my one style but unfortunately it did't work as hoped.

I used this Style

Setter setter = new Setter()
{
    Property = Control.BackgroundProperty,
    Value = null
};
Setter setter1 = new Setter()
{
    Property = Control.BorderBrushProperty,
    Value = null
};

Trigger trigger = new Trigger()
{
     Property = ListBoxItem.IsSelectedProperty,
     Value = true,
     Setters = { setter, setter1 }
};

Style style = new Style()
{
     Triggers = { trigger }
};

I also tried setting the ListView.FocusVisualStyle to null but I also had no results.

Upvotes: 1

Views: 1005

Answers (2)

tombobadil
tombobadil

Reputation: 150

Try this in your xaml code...

<ListView ...>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <ContentPresenter />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

Upvotes: 0

Zer0
Zer0

Reputation: 1166

Well there are 2 options

1) if you dont need to select items : set IsHitTestVisible to false

2) make your own style

<!--Default LitsView-->
<Style TargetType="ListView">
    <Setter Property="Background" Value="{StaticResource YOUR_BACKGROUND_COLOR}"/>
</Style>

<!--Default LitsViewItem-->
<Style x:Key="FocusVisual">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style TargetType="{x:Type ListViewItem}">
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="Padding" Value="4,1"/>
    <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <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="IsMouseOver" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" TargetName="Bd">
                            <Setter.Value>
                                <SolidColorBrush Color="{StaticResource YOUR_COLOR}" Opacity="0.3"/>
                            </Setter.Value>
                        </Setter> 
                        <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource YOUR_COLOR}"/>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="Selector.IsSelectionActive" Value="True"/>
                            <Condition Property="IsSelected" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" TargetName="Bd" Value="{StaticResource YOUR_COLOR}"/>
                        <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource YOUR_COLOR}"/>
                    </MultiTrigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Upvotes: 3

Related Questions