locked
WPF ListBox SelectedItem/SelectedIndex RRS feed

  • Question

  • I'm trying to make items in a ListBox appear "selected" (blue background white foreground). I would expect that when I set "SelectedIndex" to a non-zero value, that item in the list would appear highlighted, just like if I click on the item, but that's not the case.
    Thursday, July 28, 2011 9:04 PM

Answers

  • Hi BSavoie,

    In addition to Brian's suggestion, if you want the ListBoxItem to stay highlight when the focus moves away, you can change the ListBoxItem's styles to accomplish this.

    Here is a simple sample.

      <Window.Resources>
        <Style x:Key="MyListBoxItemStyle"
          TargetType="{x:Type ListBoxItem}">
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border Name="Bd"
                  Background="{TemplateBinding Background}"
                  BorderBrush="{TemplateBinding BorderBrush}"
                  BorderThickness="{TemplateBinding BorderThickness}"
                  Padding="{TemplateBinding Padding}"
                  SnapsToDevicePixels="true">
                  <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                         VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                         SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </Border>
                <ControlTemplate.Triggers>
                  <Trigger Property="IsSelected"
                     Value="true">
                    <Setter TargetName="Bd"
                      Property="Background"
                      Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                    <Setter Property="Foreground"
                      Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                  </Trigger>
                  <Trigger Property="IsEnabled"
                     Value="false">
                    <Setter Property="Foreground"
                      Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                  </Trigger>
                </ControlTemplate.Triggers>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>
      </Window.Resources>
      <ListBox ItemContainerStyle="{StaticResource MyListBoxItemStyle}">
      </ListBox>
    


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by BSavoie Friday, July 29, 2011 3:53 AM
    Friday, July 29, 2011 2:39 AM

All replies

  • Try this:

     

       ListBoxItem lbi = _lstBox.ItemContainerGenerator.ContainerFromItem(_lstBox.SelectedItem) as ListBoxItem;
       if (lbi != null)
        lbi.Focus();
    

     OR

          ListBoxItem lbi = _lstBox.ItemContainerGenerator.ContainerFromIndex(_lstBox.SelectedIndex) as ListBoxItem;
          if (lbi != null)
            lbi.Focus();

    Thursday, July 28, 2011 9:17 PM
  • Hi BSavoie,

    In addition to Brian's suggestion, if you want the ListBoxItem to stay highlight when the focus moves away, you can change the ListBoxItem's styles to accomplish this.

    Here is a simple sample.

      <Window.Resources>
        <Style x:Key="MyListBoxItemStyle"
          TargetType="{x:Type ListBoxItem}">
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border Name="Bd"
                  Background="{TemplateBinding Background}"
                  BorderBrush="{TemplateBinding BorderBrush}"
                  BorderThickness="{TemplateBinding BorderThickness}"
                  Padding="{TemplateBinding Padding}"
                  SnapsToDevicePixels="true">
                  <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                         VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                         SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </Border>
                <ControlTemplate.Triggers>
                  <Trigger Property="IsSelected"
                     Value="true">
                    <Setter TargetName="Bd"
                      Property="Background"
                      Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                    <Setter Property="Foreground"
                      Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                  </Trigger>
                  <Trigger Property="IsEnabled"
                     Value="false">
                    <Setter Property="Foreground"
                      Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                  </Trigger>
                </ControlTemplate.Triggers>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>
      </Window.Resources>
      <ListBox ItemContainerStyle="{StaticResource MyListBoxItemStyle}">
      </ListBox>
    


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by BSavoie Friday, July 29, 2011 3:53 AM
    Friday, July 29, 2011 2:39 AM
  • Wow, I remember the good old days when such a thing could be done with a line or two. 30 lines of code to "select" an item in a list these days. That's Progress!  :)

     

    Thanks Min

     

    Friday, July 29, 2011 3:53 AM