Reputation: 147
Ok since I'm new to WPF I'll probably ask a simple question but I've searched for a solution for the last 5 hours and found nothing.
I have a class that consist a bool value indicating if the ListView
row should be colored or not.
I'm binding the values of the rest of the columns but since there is no column for colors I'm stuck trying to figure out how to do it.
I tried Style
s, tried DataTemplate
s (it took me some time to understand where to put them and all but I figured it out) and some option with a function in C# code that I didn't know how to call.
I know I probably haven't gave enough info or any code, but let me know what you'll need to help me and I'll post it, I'm so sorry about this but again I'm new so I don't even know what you'll need.
This is the class I'm working with:
public class ReportRow
{
public string ProductID { get; set; }
public int SideA { get; set; }
public int SideB { get; set; }
public string Workers { get; set; }
public bool Equality { get; set; }
}
Everything is shown in the ListView
apart from the bool that should indicate if the row should be colored or not.
UPDATE
This is the ListView
I'm using:
<ListView Height="118" HorizontalAlignment="Right" Name="ReportView" VerticalAlignment="Top" Width="390" HorizontalContentAlignment="Right" Grid.Row="1" ItemsSource="{Binding ReportRows}" >
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="Product" Width="60" DisplayMemberBinding="{Binding ProductID}" />
<GridViewColumn Header="SideA" Width="60" DisplayMemberBinding="{Binding SideA}"/>
<GridViewColumn Header="SideB" Width="60" DisplayMemberBinding="{Binding SideB}"/>
<GridViewColumn Header="Workers" DisplayMemberBinding="{Binding Workers}"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
This is what I currently have.
Upvotes: 3
Views: 1131
Reputation: 6014
You can color your ListViewItems with the ItemContainerStyle-property:
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Equality}" Value="true">
<Setter Property="Background" Value="ColorWhenTrue"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Equality}" Value="false">
<Setter Property="Background" Value="ColorWhenFalse"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
Upvotes: 2
Reputation: 12295
Hi try it using Converter
public class BoolToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is bool && (bool)value)
return new SolidColorBrush(Colors.Red);
else
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
<Window.Resources>
<conv:BoolToColorConverter x:Key="boolToColorConverter"/>
</Window.Resources>
<GridViewColumn Header="Product" Width="60" DisplayMemberBinding="{Binding ProductID}" BackGround="{Binding Equality, Converter={StaticResource boolToColorConverter}}" />
I hope this will give you an idea.
Upvotes: 2
Reputation: 2691
Here is a link for a project that demonstrates what I believe you're looking for: http://www.codeproject.com/Articles/18585/Highlighting-Items-in-a-WPF-ListView. You will need to change the code for the converter code since your property is boolean and not an integer and change the binding to your property name.
Upvotes: 0