none
ListBox Foreground Color Problem for Selected Item Without ItemContainer RRS feed

  • Question

  • <ListBox SelectionMode="Multiple">
    
    	<StackPanel>
    		<TextBlock>Selection is Black</TextBlock>
    	</StackPanel>
    	
    	<ListBoxItem>
    		<StackPanel>
    		<TextBlock>Selection is White</TextBlock>
    		</StackPanel>
    	</ListBoxItem>
    </ListBox>
    

    The above source looks like this when you select all items.



    Now I want to know why the first item which is not its own itemcontainer, stays black, and second item which is its own item container, turns white as expected.
    Wednesday, December 9, 2009 4:51 PM

Answers

  • Hi Akash,

    I performed a test on your sample code and did reproduce the problem on my side.

    I use XamlPad to look into the first ListBoxItem in the ListBox and see that the Foreground of this ListBoxItem does change to White when I select it. But the nested TextBlock's Foreground doesn't change. I don't know why, but a workaround is to bind the Foreground of the TextBlock to the Foreground property of the item container. For example:

      <ListBox SelectionMode="Multiple">

                <StackPanel>
                    <TextBlock  Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}, Path=Foreground}">Selection is Black</TextBlock>
                </StackPanel>

                <ListBoxItem>
                    <StackPanel>
                        <TextBlock>Selection is White</TextBlock>
                    </StackPanel>
                </ListBoxItem>
     </ListBox>

    Hope this helps.

    Sincerely,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Linda Liu Monday, December 14, 2009 3:10 AM
    Friday, December 11, 2009 11:04 AM

All replies

  • Could you explain your scenario why don't you use ListBoxItem to wrap the StackPanel to get the behavior you want?  Thanks.
    William
    Thursday, December 10, 2009 4:43 AM
  • Hi Akash,

    I performed a test on your sample code and did reproduce the problem on my side.

    I use XamlPad to look into the first ListBoxItem in the ListBox and see that the Foreground of this ListBoxItem does change to White when I select it. But the nested TextBlock's Foreground doesn't change. I don't know why, but a workaround is to bind the Foreground of the TextBlock to the Foreground property of the item container. For example:

      <ListBox SelectionMode="Multiple">

                <StackPanel>
                    <TextBlock  Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}, Path=Foreground}">Selection is Black</TextBlock>
                </StackPanel>

                <ListBoxItem>
                    <StackPanel>
                        <TextBlock>Selection is White</TextBlock>
                    </StackPanel>
                </ListBoxItem>
     </ListBox>

    Hope this helps.

    Sincerely,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Linda Liu Monday, December 14, 2009 3:10 AM
    Friday, December 11, 2009 11:04 AM