Вопрос

I simplified my problem with a sample application. So I have a list with some objects represented by a rectangle.

But I have a problem if my application turn on Windows 8. A blue rectangle appears around my rectangles when I place my mouse over the element. I tried some solutions found on the internet, but none of them work.

My code :

<Window x:Class="Test_application.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Test_application"
    Title="MainWindow" Height="350" Width="525">
<Grid Background="DarkGray">
    <ListBox Margin="10" x:Name="lbTest">
        <ListBox.Resources>
            <DataTemplate DataType="{x:Type local:Segment}">
                <Rectangle Width="150" Height="10" Stroke="Gray" StrokeThickness="1.354" Margin="10"/>
            </DataTemplate>
        </ListBox.Resources>
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Canvas.Left" Value="{Binding X}"/>
                <Setter Property="Canvas.Top" Value="{Binding Y}"/>
                <Setter Property="Margin" Value="0" />
                <Setter Property="Focusable" Value="False" />
                <Setter Property="Padding" Value="0" />
                <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="Background" Value="{x:Null}" />
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
</Grid>

Application

Это было полезно?

Решение

The selection and mouseover colors are defined in Triggers in the ControlTemplate for ListBoxItem so you'll need to customize that if you want to use different (or no) color for them. Depending on how much of the original trigger functionality you want you can make a copy of the original one (Blend will do this for you) and modify it or just use something simple like this:

        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Canvas.Left" Value="{Binding X}"/>
                <Setter Property="Canvas.Top" Value="{Binding Y}"/>
                <Setter Property="Margin" Value="0" />
                <Setter Property="Focusable" Value="False" />
                <Setter Property="Padding" Value="0" />
                <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="Background" Value="{x:Null}" />
                <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}">
                                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>

Alternatively, you should consider whether you actually need a ListBox or could just use a base ItemsControl, which has none of the mouseover behavior you're trying to get rid of. The decision should be based on whether or not you need item selection, as this is the primary thing that ListBox adds.

Другие советы

Default highlight can be overriden.

<ListView.Resources>
    ...
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Orange" />
    ...
</ListView.Resources>

Instead Orange color set Transparent or #00FFFFFF.

EDIT:

Or add trigger to ItemContainerStyle

<Style TargetType="{x:Type ListBoxItem}">
    ...
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Transparent" />
        </Trigger>
    </Style.Triggers>       
    ...
</Style>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top