Here's one way to style a listview item/row using Converter and Triggers.
I assume that you have successfully bind an observable collection object to a listview control.
Converter class:
Code:
public class AddressTargetConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (value.ToString().Equals(parameter.ToString()));
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
XAML:
Code:
<Window.Resources>
<y:AddressTargetConverter x:Key="AddressTargetConverter" />
</Window.Resources>
<Grid>
<StackPanel>
<ListView ItemsSource="{Binding PersonCollection}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Style.Triggers>
<DataTrigger Binding="{Binding Address, Converter={StaticResource AddressTargetConverter}, ConverterParameter = Guam}" Value="True">
<Setter Property="Background" Value="LightBlue"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Width="140" Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Width="200" Header="Address" DisplayMemberBinding="{Binding Address}" />
<GridViewColumn Width="160" Header="Contact" DisplayMemberBinding="{Binding Contact}" />
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</Grid>
Note:
- This will change the row/item color in the listview control if the Address is Guam.
- Name, Address and Contact are string properties in Person class.
- add this declaration
Code:
xmlns:y="clr-namespace:DemoListview"
where DemoListview is my project namespace.