Search code examples
wpfxamlwindows-runtimewindows-phone-8.1windows-phone

Windows Phone 8.1 highlight titles of some list elements by parameter


I would like to highlight titles of my listview elements, but not all. Each element has a property which decides whether the title will be bold. I use MVVM. How do this?


Solution

  • DataTemplate

    You can make a datatemplate for these listview items which can change the style based on the property, combine this with a converter.

            <ListView ItemsSource="{Binding ListOfElements}">
                <ListView.ItemTemplate>
                    <DataTemplate>  
                            <TextBlock Text="{Binding Title}"
                               FontWeight="{Binding IsBold, Converter={StaticResource BoolToFontWeightConverter}}"/>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    

    Then all you need to do is add a converter which returns the appropriate Font Weight for each item based off the property. You could also put this data template into a resource dictionary at the top of your file or in another file and then reference it by name if you wanted to.