wpf listview selection

WPF ListView Inactive Selection Color



(6)

Estoy creando una aplicación WPF donde varias selecciones de ListView se hacen en una fila (similar al navegador de iTunes). El problema es que el color de selección inactivo predeterminado es demasiado claro. (vea abajo)

¿Cómo puedo cambiar este color para que mi vista de lista inactiva se vea así? (vea abajo)

Solución

Reemplace el SystemColor predeterminado con un Style como ese:

<Style TargetType="ListViewItem"> <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static SystemColors.HighlightColor}"/> </Style.Resources> </Style>


Cambiar SystemColors.ControlBrushKey no funcionó para mí, tuve que cambiar SystemColors.InactiveSelectionHighlightBrushKey

Entonces, en lugar de:

<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Red" />

Tuve que usar:

<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Red"/>


Debe anular algunas propiedades de SystemColors. Eche un vistazo a SystemColors Class (MSDN) . Hay más propiedades que InactiveSelectionHighlightBrushKey, por ejemplo InactiveSelectionHighlightTextBrushKey que afecta el color del texto.

<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>


En los Frameworks anteriores de .NET, la anulación de los colores del sistema no funciona. La solución que funciona en .NET Framework 4.0 está here .

<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>

Funciona tanto para ListBox como para ListView.


La plantilla ListBox utiliza un color de sistema llamado ControlBrush para establecer el color de resaltado inactivo. Por lo tanto, puedes anular ese color:

<ListBox> <ListBox.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}">Red</SolidColorBrush> </ListBox.Resources> </ListBox>


La respuesta en algunos casos resolverá el problema, pero no es ideal, ya que se rompe cuando el control está desactivado / solo de lectura y también anula las combinaciones de colores, en lugar de aprovecharlas. Mi sugerencia es agregar lo siguiente en las etiquetas de ListBox:

<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>

Lo que hará es establecer el color de fondo Resaltar en el elemento del cuadro de lista siempre que se seleccione (independientemente del estado de control).

Mi respuesta se basa en la ayuda de la respuesta ya dada, junto con el siguiente blog: http://blogs.vbcity.com/xtab/archive/2009/06/29/9344.aspx


Para mí, esto funcionó:

<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>