Reputation: 4375
I would like to fill a WPF ListView with strings using a databinding. My code looks like this (and work! ;) ):
Xaml:
<ListView
ItemsSource="{Binding Entries}">
</ListView>
I left out some code for a better overview.
Entries is an IList<string>
.
So far, everything is working fine. Now comes the problem: A string in Entries may contain a particular keyword that indicates that this string wants to be displayed with red background inside the ListView. Say we have a method GetBackground(string s) which returns a Colour depending on the string.
How can I make the ListView display its items in the correct colour. My first idea was to have a converter converting my string to a colour using the abovementioned method. Where would I have to add this converter and how do I pass the string to the converter as parameter? my first Idea was:
<ListView
ItemsSource="{Binding Entries, Converter={StaticResource entryToColourConverter},
ConverterParameter=???}"
</ListView>
Does anyone have an idea how this could be done? Am I on the right track?
Best wishes, Christian
EDIT 1: Changed the code (as a first step) towards:
<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 change the text to some static value, the result is still the same as with the previous code.
EDIT 2: I found the 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 the problem. On removing that, everything was working! :) Now I have to find out how to solve this without the ListView.View
Upvotes: 3
Views: 3884
Reputation: 8295
<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>
Converter:
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;
}
}
You're on the right lines... something like this should work...
Upvotes: 4
Reputation: 7744
Create item template for ListView and then tie text color of (for example) label to Entrie property
Upvotes: 0