WPF ListView sélection des couleurs inactives
je crée une application WPF où plusieurs sélections ListView sont faites dans une rangée (similaire au navigateur iTunes). Le problème est que la couleur de sélection inactive par défaut est trop légère. (voir ci-dessous)
Comment changer cette couleur pour que mon listview inactif ressemble à ça? (voir ci-dessous)
Solution
Outrepasser le SystemColor par défaut avec un Style
comme ceci:
<Style TargetType="ListViewItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static SystemColors.HighlightColor}"/>
</Style.Resources>
</Style>
6 réponses
le modèle ListBox
utilise une couleur de système appelée ControlBrush
pour définir la couleur de surlignage inactive. Par conséquent, vous pouvez simplement remplacer cette couleur:
<ListBox>
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}">Red</SolidColorBrush>
</ListBox.Resources>
</ListBox>
changer SystemColors.ControlBrushKey
n'a pas fonctionné pour moi, j'ai dû changer
SystemColors.InactiveSelectionHighlightBrushKey
donc au lieu de:
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Red" />
j'ai dû utiliser:
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Red"/>
la réponse résoudra dans certains cas le problème, mais elle n'est pas idéale car elle casse lorsque la commande est désactivée/en lecture seule et elle l'emporte aussi sur les couleurs, plutôt que d'en profiter. Ma suggestion est d'ajouter ce qui suit dans les balises ListBox à la place:
<ListBox....>
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" Padding="2" SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background"
Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Resources>
</ListBox>
ce que cela va faire est de définir la couleur de fond en surbrillance sur l'élément de la boîte de liste chaque fois qu'il est sélectionné (indépendamment de l'état de contrôle).
ma réponse est basé sur l'aide de la réponse déjà donnée, avec le blog suivant: http://blogs.vbcity.com/xtab/archive/2009/06/29/9344.aspx
vous devez modifier certaines propriétés de SystemColors. Jetez un oeil à SystemColors Class (MSDN) . Il y a plus de propriétés que InactiveSelectionHighlightBrushKey, par exemple Inactiveselectionhighlightttttbrushkey qui affecte la couleur du texte.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White"/>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Yellow"/>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="Blue"/>
<Style TargetType="ListViewItem">
<Setter Property="FontSize" Value="20" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Padding" Value="25,5" />
</Style>
</Window.Resources>
<StackPanel Orientation="Horizontal">
<ListView>
<ListViewItem Content="Item" />
<ListViewItem Content="Item" />
</ListView>
<ListView>
<ListViewItem Content="Item" />
<ListViewItem Content="Item" />
</ListView>
</StackPanel>
</Window>
pour moi, cela a fait l'affaire:
<ListBox HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Margin="-5, -2,-5,-2" Content="{Binding Item}">
<Label.Style>
<Style TargetType="Label">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBox}},Path=IsFocused}" Value="False"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True"/>
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="CornflowerBlue"/>
</MultiDataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True">
<Setter Property="Foreground" Value="White"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="False">
<Setter Property="Foreground" Value="Black"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
dans les anciennes couleurs de système .net Frameworks overriding ne fonctionne pas. La Solution qui fonctionne dans .net Framework 4.0 est ici .
<ListView>
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}">
<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="Selector.IsSelectionActive"
Value="False" />
<Condition Property="IsSelected"
Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Background"
TargetName="Bd"
Value="DarkOrange" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive"
Value="True" />
<Condition Property="IsSelected"
Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Background"
TargetName="Bd"
Value="OrangeRed" />
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
fonctionne à la fois pour ListBox et ListView.