Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. ListBox Style Problem

ListBox Style Problem

Scheduled Pinned Locked Moved WPF
helpdatabasequestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I have created a style for a listbox: ItemContainerStyle

    <Setter Property="Padding" Value="2"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Foreground" Value="Black"/>
    
    <Style.Triggers>
    
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="Orange"/>
            <Setter Property="Foreground" Value="Blue"/>
        </Trigger>
            
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Orange"/>
            <Setter Property="Foreground" Value="Blue"/>
        </Trigger>
            
    </Style.Triggers>
    

    ListBoxStyle

    <Setter Property="Foreground" Value="Red"/>
    <Setter Property="Background" Value="White"/>
    <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBackgroundBrush}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="ItemContainerStyle" Value="{StaticResource listBoxItemContainerStyle}"/>
        
    <Style.Triggers>
    
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="BorderBrush" Value="{StaticResource ButtonDisabledBackgroundBrush}"/>
            <Setter Property="Control.BorderThickness" Value="2"/>
        </Trigger>
    
        <Trigger Property="IsKeyboardFocusWithin" Value="True">
            <Setter Property="IsSelected" Value="True"/>
        </Trigger>
    
    </Style.Triggers>
    

    Usage

    When I run it BEFORE I click an item, the background of the first item is lightgray. Notice that the first item has IsSeledted= true. If I click on it, then the background color turns Blue. What's wrong here?

    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

    H 1 Reply Last reply
    0
    • K Kevin Marois

      I have created a style for a listbox: ItemContainerStyle

      <Setter Property="Padding" Value="2"/>
      <Setter Property="Background" Value="Transparent"/>
      <Setter Property="Foreground" Value="Black"/>
      
      <Style.Triggers>
      
          <Trigger Property="IsSelected" Value="True">
              <Setter Property="Background" Value="Orange"/>
              <Setter Property="Foreground" Value="Blue"/>
          </Trigger>
              
          <Trigger Property="IsMouseOver" Value="True">
              <Setter Property="Background" Value="Orange"/>
              <Setter Property="Foreground" Value="Blue"/>
          </Trigger>
              
      </Style.Triggers>
      

      ListBoxStyle

      <Setter Property="Foreground" Value="Red"/>
      <Setter Property="Background" Value="White"/>
      <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBackgroundBrush}"/>
      <Setter Property="BorderThickness" Value="1"/>
      <Setter Property="ItemContainerStyle" Value="{StaticResource listBoxItemContainerStyle}"/>
          
      <Style.Triggers>
      
          <Trigger Property="IsEnabled" Value="False">
              <Setter Property="BorderBrush" Value="{StaticResource ButtonDisabledBackgroundBrush}"/>
              <Setter Property="Control.BorderThickness" Value="2"/>
          </Trigger>
      
          <Trigger Property="IsKeyboardFocusWithin" Value="True">
              <Setter Property="IsSelected" Value="True"/>
          </Trigger>
      
      </Style.Triggers>
      

      Usage

      When I run it BEFORE I click an item, the background of the first item is lightgray. Notice that the first item has IsSeledted= true. If I click on it, then the background color turns Blue. What's wrong here?

      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

      H Offline
      H Offline
      Henrik Jonsson
      wrote on last edited by
      #2

      Hello, the problem is that the default control template for ListBoxItem contains a border element which controls the background color. To fully control the appearance you could replace the template using the template from ListBoxItem ControlTemplate Example[^] with your own definition. See below for an example.

      <Style x:Key="listBoxItemContainerStyle" TargetType="{x:Type ListBoxItem}">
      <Setter Property="SnapsToDevicePixels" Value="true"/>
      <Setter Property="OverridesDefaultStyle" Value="true"/>
      <Setter Property="Foreground" Value="Black"/>
      <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="Orange"/>
      <Setter Property="Foreground" Value="Blue"/>
      </Trigger>
      <Trigger Property="IsEnabled" Value="false">
      <Setter Property="Foreground" Value="Gray"/>
      <Setter TargetName="Border" Property="BorderThickness" Value="2"/>
      </Trigger>
      <Trigger Property="IsMouseOver" Value="true">
      <Setter TargetName="Border" Property="Background" Value="Orange"/>
      <Setter Property="Foreground" Value="Blue"/>
      </Trigger>
      </ControlTemplate.Triggers>
      </ControlTemplate>
      </Setter.Value>
      </Setter>
      </Style>

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups