locked
ListBox item selected foreground color not working if initial foreground color altered. What am I missing??? RRS feed

  • Question

  • Hello,

    I have a ListBox where I needed some of my items listed and red and the rest in the normal black.  I was able to do that by adding the following:

     <ListBox.Resources>
          <DataTemplate DataType="{x:Type ViewModels:IngestGroup}">
                <TextBlock Text="{Binding Path=name}">
                     <TextBlock.Foreground>
                           <SolidColorBrush Color="{Binding Path=color}"/>
                     </TextBlock.Foreground>
                </TextBlock>
           </DataTemplate>
     </ListBox.Resources>

    All is well with that.  When an item is selected, however, the normal selected foreground color of white is not used.  The foreground color remains either black or red.  So, I tried to add the following to give me the white foreground on the selected node:

    <ListBox.ItemContainerStyle>
         <Style TargetType="ListBoxItem">
               <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True" >
                        <Setter Property="Foreground" Value="White" />
                    </Trigger>
               </Style.Triggers>
         </Style>
    </ListBox.ItemContainerStyle>

    This has no effect and the selected foreground color is never changed. When I leave this code and remove the <ListBox.Resources> entry, the trigger DOES chnage the foreground color to the designated value. I tried this using various colors since white is the default.

    Does anyone know why the trigger setter property is not changed when the ListBox initially sets text color via the <DataTemplate>??  Is there a different way I should be doing this.

    Thanks - Peter

    Saturday, March 16, 2013 2:23 AM

Answers

  • Hi Peter,

    Both of the styles should be applied to ListBoxItem. Please have a look at the following code:

             <ListBox x:Name="listbox1" >           
                <ListBox.ItemTemplate>
                        <DataTemplate>                        
                            <TextBlock Text="{Binding Path=name}" >                            
                            </TextBlock>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="Foreground" Value="{Binding Path=brush}">
                        </Setter>
                        <Style.Triggers>
                            <Trigger Property="ListBoxItem.IsSelected" Value="True" >
                                <Setter Property="Foreground" Value="Pink" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </ListBox.ItemContainerStyle>  
            </ListBox>

        public partial class ListBox : Window
        {
            
            public ListBox()
            {
                InitializeComponent();
    
                List<Test> list = new List<Test>();
                list.Add(new Test("AA",  new SolidColorBrush(Colors.Red)));
                list.Add(new Test("BB", new SolidColorBrush(Colors.Blue)));
                list.Add(new Test("CC", new SolidColorBrush(Colors.Blue)));
                list.Add(new Test("DD",  new SolidColorBrush(Colors.Black)));
                list.Add(new Test("EE", new SolidColorBrush(Colors.Blue)));
                list.Add(new Test("FF", new SolidColorBrush(Colors.Blue)));
                listbox1.ItemsSource = list;
                listbox1.Items.Refresh();         
            }
    
    
        }
    
        public class Test
        {
           public string name { get; set; }  
           public SolidColorBrush brush { get; set; }
    
           public Test(string s,  SolidColorBrush b)
           {
               name = s;
               brush = b;
           }
        }

    Have a nice day.


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Edited by Lisa Zhu Tuesday, March 19, 2013 1:34 PM
    • Marked as answer by Peter Len Tuesday, March 19, 2013 4:13 PM
    Tuesday, March 19, 2013 1:13 PM

All replies

  • Hi Peter,

    Both of the styles should be applied to ListBoxItem. Please have a look at the following code:

             <ListBox x:Name="listbox1" >           
                <ListBox.ItemTemplate>
                        <DataTemplate>                        
                            <TextBlock Text="{Binding Path=name}" >                            
                            </TextBlock>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="Foreground" Value="{Binding Path=brush}">
                        </Setter>
                        <Style.Triggers>
                            <Trigger Property="ListBoxItem.IsSelected" Value="True" >
                                <Setter Property="Foreground" Value="Pink" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </ListBox.ItemContainerStyle>  
            </ListBox>

        public partial class ListBox : Window
        {
            
            public ListBox()
            {
                InitializeComponent();
    
                List<Test> list = new List<Test>();
                list.Add(new Test("AA",  new SolidColorBrush(Colors.Red)));
                list.Add(new Test("BB", new SolidColorBrush(Colors.Blue)));
                list.Add(new Test("CC", new SolidColorBrush(Colors.Blue)));
                list.Add(new Test("DD",  new SolidColorBrush(Colors.Black)));
                list.Add(new Test("EE", new SolidColorBrush(Colors.Blue)));
                list.Add(new Test("FF", new SolidColorBrush(Colors.Blue)));
                listbox1.ItemsSource = list;
                listbox1.Items.Refresh();         
            }
    
    
        }
    
        public class Test
        {
           public string name { get; set; }  
           public SolidColorBrush brush { get; set; }
    
           public Test(string s,  SolidColorBrush b)
           {
               name = s;
               brush = b;
           }
        }

    Have a nice day.


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Edited by Lisa Zhu Tuesday, March 19, 2013 1:34 PM
    • Marked as answer by Peter Len Tuesday, March 19, 2013 4:13 PM
    Tuesday, March 19, 2013 1:13 PM
  • Lisa,

    Yes, that did it.  The items that I was using worked well individually but not together.  Your version does the job.  Thanks!!

    - Peter

    Tuesday, March 19, 2013 4:13 PM