Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom styled listbox - how can I keep the style for a selected item?
    primarykey
    data
    text
    <p>I have a styled listbox. Listbox items change color when hovered over and when selected. Hover and select work fine. But when selecting an item then taking the mouse off it and coming back to hover over it causes it to go back to hover/unselected state even though it is still selected. How can I keep the listboxitem in a "selected" visual state?</p> <pre><code>&lt;Style x:Name="myListBoxItemStyle" TargetType="ListBoxItem"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="ListBoxItem"&gt; &lt;Border x:Name="myBorder" CornerRadius="5" BorderThickness="3" Background="#FF292121" Margin="0"&gt; &lt;Grid HorizontalAlignment="Stretch"&gt; &lt;ContentPresenter Content="{TemplateBinding Content}" Margin="5,0,5,0" /&gt; &lt;/Grid&gt; &lt;VisualStateManager.VisualStateGroups&gt; &lt;VisualStateGroup x:Name="CommonStates"&gt; &lt;VisualState x:Name="Normal"/&gt; &lt;VisualState x:Name="MouseOver"&gt; &lt;Storyboard&gt; &lt;ColorAnimation Storyboard.TargetName="myBorder" Storyboard.TargetProperty="(Background).(SolidBrush.Color)" Duration="00:00:00.2" To="#FF949290" /&gt; &lt;/Storyboard&gt; &lt;/VisualState&gt; &lt;VisualState x:Name="Disabled"/&gt; &lt;/VisualStateGroup&gt; &lt;VisualStateGroup x:Name="SelectionStates"&gt; &lt;VisualState x:Name="Unselected" /&gt; &lt;VisualState x:Name="Selected"&gt; &lt;Storyboard&gt; &lt;ColorAnimation Storyboard.TargetName="myBorder" Storyboard.TargetProperty="(Background).(SolidBrush.Color)" Duration="00:00:00.2" To="#FF949290" /&gt; &lt;/Storyboard&gt; &lt;/VisualState&gt; &lt;VisualState x:Name="SelectedUnfocused"/&gt; &lt;/VisualStateGroup&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;Style x:Key="myListBoxStyle" TargetType="ListBox"&gt; &lt;Setter Property="Background" Value="Transparent"/&gt; &lt;Setter Property="Margin" Value="0"/&gt; &lt;Setter Property="BorderThickness" Value="0"/&gt; &lt;Setter Property="ItemContainerStyle" Value="{StaticResource myListBoxItemStyle}"/&gt; &lt;Setter Property="VerticalAlignment" Value="Bottom"/&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="ListBox"&gt; &lt;Grid Margin="0"&gt; &lt;ItemsPresenter /&gt; &lt;/Grid&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;Setter Property="ItemsPanel"&gt; &lt;Setter.Value&gt; &lt;ItemsPanelTemplate&gt; &lt;StackPanel Orientation="Horizontal"/&gt; &lt;/ItemsPanelTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;ListBox Name="theControl" Style="{StaticResource myListBoxStyle}" /&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload