Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9160065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:38:49+00:00 2026-06-17T13:38:49+00:00

I’m trying to create a listbox that shows a thumbnail view of page content,

  • 0

I’m trying to create a listbox that shows a thumbnail view of page content, for an app with one canvas but multiple ‘pages’. Yes, that’s probably not the best place to start from but for historical reasons that’s what I have.
I’ve implemented the ListBox with data binding to a singleton ‘WorkBook’ that has an ObservableCollection of PageData (everything that appears on a page, including it’s background).
What I really really want is to be able to change the Border colour of a ListBoxItem when it’s selected and keep that Border colour while it’s content is the currently selected item in the class that hosts the collection.

My problems are:-
1/ I can’t get the ListBox to select the 1st item on program startup.

2/ when the ListBox loses focus the SelectedIndex is always -1 (so no selection)

3/ adding to the ListBox results in no selection (SelectedIndex == -1)

4/ using triggers, I can set the border of the selectedItem but this is lost when the ListBox loses focus. As the ListBoxItem shows an image that is opaque the ‘standard’ way of making selection colours stay when out of focus doesn’t work – ie

<Style x:Key="PsThumb">
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="White"></SolidColorBrush>
                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="White"></SolidColorBrush>
                </Style.Resources>
</Style>

My code for the ListBox is as follows :-

<ListBox  x:Name="PageSorter" Style="{StaticResource PsThumb}"  Width="148" BorderThickness="4" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
        VerticalAlignment="Stretch" ItemsSource="{Binding Pages,Source={StaticResource WorkBook}}" SelectedItem="{Binding Path=CurrentPageData, Mode=TwoWay}" 
         AllowDrop="True" ScrollViewer.VerticalScrollBarVisibility="Auto">
                <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                         <Border x:Name="border" BorderBrush="DarkGray" BorderThickness="4" Margin="2,4,2,4" CornerRadius="5">
                             <Border.Effect>
                                 <DropShadowEffect ShadowDepth="6"/>
                             </Border.Effect>
                             <Image Source="{Binding Thumbnail}" Width="130" Height="95" Stretch="Fill"/>
                         </Border>
                    </Grid>
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True">
                                <Setter TargetName="border" Property="BorderBrush" Value="Red"></Setter>
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:38:51+00:00Added an answer on June 17, 2026 at 1:38 pm

    The simplest way to achieve what you’re asking is to bind the DataTrigger to a property inside the items of the ListBox. In the example below, I added the Selected property on my Contact class:

    public class Contact : INPCBase
    {
        public string Name { get; set; }
    
        private bool _Selected;
        public bool Selected 
        {
            get { return _Selected; }
            set
            {
                _Selected = value;
                NotifyPropertyChanged("Selected");
            }
        }
    }
    

    I then bind my ListBox to a List<Contact>. When the user checks the CheckBox, we change the Selected property and in turn trigger the Style:

    <Window x:Class="WpfApp.ListboxKeepSelectionWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ListboxKeepSelectionWindow" Height="277" Width="343"
        xmlns:me="clr-namespace:WpfApp">
    
    <Window.Resources>
        <me:ContactList x:Key="sample"/>
    
        <Style TargetType="ListBoxItem" x:Key="SelectedListBoxItemStyle">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Selected}" Value="True">
                    <Setter Property="BorderBrush" Value="Orange"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    
    <Grid>
        <ListBox Name="lbxContacts"
                 ItemsSource="{StaticResource ResourceKey=sample}"
                 SelectionMode="Extended"
                 ItemContainerStyle="{StaticResource ResourceKey=SelectedListBoxItemStyle}" 
                 SelectionChanged="lbxContacts_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <CheckBox IsChecked="{Binding Path=Selected}">
                            <TextBlock Text="{Binding Path=Name}"/>
                        </CheckBox>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
    

    To allow the user to select items without using the checkbox, I added this small event on the ListBox. Since the Contact class implements the INotifyPropertyChanged interface, the value of the CheckBox is updated so the user knows the selection worked:

        private void lbxContacts_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (Contact c in e.AddedItems)
            {
                c.Selected = !c.Selected;
            }
        }
    

    When you want to get a list of selected items, you just use a LINQ query on the ItemsSource to get items where Selected == true.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am using JSon response to parse title,date content and thumbnail images and place
I have a French site that I want to parse, but am running into
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.