How to display strings in WPF ListView in different colors?

I would like to populate a WPF ListView with rows using data binding. My code looks like this (and it works !;)):

Xaml:

<ListView 
            ItemsSource="{Binding Entries}">
</ListView>

I left some code for a better overview. Entries - IList<string>.

So far, everything is working fine. Now the problem is: the row in the record may contain a specific keyword that indicates that the row should be displayed in red inside the ListView. Say we have a GetBackground method (string s) that returns color depending on the string.

How can I make ListView display its elements in the correct color. My first idea was for the converter to convert my string to color using the above method. Where should I add this converter and how to pass a string to the converter as a parameter? my first idea:

<ListView
    ItemsSource="{Binding Entries, Converter={StaticResource entryToColourConverter}, 
ConverterParameter=???}"
</ListView>

Does anyone have an idea how to do this? Am I on the right track?

Best regards, Christian

EDIT 1: Changed code (as a first step) in the direction of:

<UserControl.Resources>
        <DataTemplate x:Key="entryTemplate">
            <TextBlock 
                Text="{Binding}"
                Background="Green"/>
        </DataTemplate>
</UserControl.Resources>

...

<ListView 
            ItemsSource="{Binding Entries}"
            ItemTemplate="{StaticResource entryTemplate}>
</ListView>

However, this does not work at all. Even if I changed the text to some static value, the result is still the same as in the previous code.

EDIT 2: I found a problem, my code looked like this:

<ListView x:Name="lvEntries"
                  ItemTemplate="{StaticResource EntriesTemplate}"
                  ItemsSource="{Binding Entries, NotifyOnTargetUpdated=True}">

            <ListView.View>

                <GridView x:Name="gvEntries">
                    <GridViewColumn
                        HeaderContainerStyle="{StaticResource hcs}"
                        Header="Entry"
                        TextBlock.TextAlignment="Left">
                    </GridViewColumn>
                </GridView>
            </ListView.View>

        </ListView>

And this ListView.View was a problem. Everything works on it! :) Now I have to figure out how to solve this without ListView.View

+3
2
<Window.Resources>
    <local:EntryToBackgroundConverter x:Key="EntryToBackgroundConverter"/>
    <DataTemplate x:Key="EntryTemplate">
        <TextBlock Text="{Binding .}" Background="{Binding ., Converter={StaticResource EntryToBackgroundConverter}}"/>
    </DataTemplate>        
</Window.Resources>
<Grid>              
    <ListView ItemsSource="{Binding Entries}" ItemTemplate="{StaticResource EntryTemplate}"></ListView>        
</Grid>

public class EntryToBackgroundConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string stringValue = value as string;

        if (string.IsNullOrEmpty(stringValue))
            return Brushes.Black;

        if (stringValue == "foreach")
            return Brushes.Blue;
        if (stringValue == "if")
            return Brushes.Blue;

        return Brushes.Black;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }
}

... - ...

+4

ListView, () Entrie

0

Source: https://habr.com/ru/post/1792996/


All Articles