none
Changing Foreground of a TextBlock in a ListBoxItem RRS feed

  • Question

  • I have a ListBox lb with ItemTemplate set to a data template. This data template is like:

    <StackPanel Orientation="Horizontal">  
      <TextBlock x:Name="tb1" Text="TextBlock1" />  
      <TextBlock x:Name="tb2" Text="TextBlock2" />  
    </StackPanel> 

    Now, whenever I touch the item, only tb1 gets focussed, so its foreground changes. OK. But sometimes tb1.Text will be empty. Then there will be no indication of the selected item. tb2 will always have some text.

    How can set the focus to tb2 when the item is selected? Basically, its Foreground should change when the item is selected.
    Monday, December 13, 2010 8:35 PM

All replies

  • Hi ESM,

    When you say "the item is selected" which control are you refering to?
    Monday, December 13, 2010 11:14 PM
  • Hi Mick,

    I meant ListBoxItem.
    Tuesday, December 14, 2010 12:13 AM
  • Ah yes, that make senses... and you did mention ListBoxItem in the title. This thread might help you with that.

    Change the background of ListBox item in selected and mouse hover in windows phone 7

    There's some good input there, including some sample code Richard verified works.
    Tuesday, December 14, 2010 12:45 AM
  • Hi Mick,

    Thanks, I have implemented it.

    But actually, that is not what I was asking for. If you read my original post, I wanted to change the Foreground of a TextBlock that is contained within a ListBoxItem. As my deadline is approaching, I implemented the 'change the Background of the ListBoxItem' method to get going. The method you indicated is for setting a ListBoxItem as a whole. How to change a control wihtin that Item?
    Tuesday, December 14, 2010 8:29 PM