Search code examples
windows-phone-7listboxsilverlight-3.0foreground

Change the color of some items inside a databinded listbox


Inside my Windows Phone 7 application I have a list box with a list of 50 authors. I want to display the first 5 items of my list box with a white foreground (or black depending of theme), and the rest of them in gray.

<ListBox x:Name="AuthorsListBox" ItemsSource="{Binding}" Grid.Row="1"    >
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="{Binding Name}" 
                                  Name="{Binding Id}" Width="320" Height="70"
                                  TextWrapping="Wrap" TextAlignment="Left" 
                                  Margin="0,0,0,10"  FontSize="30" 
                                  ManipulationCompleted="Author_ManipulationCompleted"/>                                 
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
</ListBox>

Here is my C# code:

AuthorsListBox.ItemsSource = AuthorsList.OrderBy(a => a.Name);

Solution

  • Bind the Foreground property of the Textblock to the desired colour. Or to an indictor and use a converter to select the colour based on the indicator.