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);
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.
I would add an "Onitemdatabound" to the listbox and then set the colour in that method
精彩评论