Hi!
I want to alternate the background of the listviewitems by binding a converter to the background border of each listview item.
listview.xaml:
<l:cBackgroundConvertor x:Key="myConverter"/>
<Style x:Key="{x:Type ListViewItem}" TargetType="ListViewItem">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="Height" Value="45"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border Name="Border" Padding="2" SnapsToDevicePixels="true">
<Border.Background>
<SolidColorBrush Color="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource myConverter}}" />
</Border.Background>
<GridViewRowPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Foreground" Value="DarkBlue"/>
<Setter TargetName="Border" Property="Background" Value="{StaticResource DarkYellowBrush}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Foreground" Value="DarkBlue"/>
<Setter TargetName="Border" Property="Background" Value="{StaticResource MouseOverBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<...>
my converter class:
namespace GesHoras.Classes { public sealed class cBackgroundConvertor : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { ListViewItem item = (ListViewItem)value; ListView listView = ItemsControl.ItemsControlFromItemContainer(item) as ListView;
// Get the index of a ListViewItem
int index = listView.ItemContainerGenerator.IndexFromContainer(item);
if (index % 2 == 0)
{
return Brushes.LightBlue;
}
else
{
return Brushes.Beige;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
}
I get an error message in runtime (it's in Spanish sorry, it says it cannot convert 'System.Windows.Media.SolidColorBrush' in 'System.Windows.Controls.ListViewItem':
El código de usuario no controló System.InvalidCastException Message="No se puede convertir un objeto de tipo 'System.Windows.Media.SolidColorBrush' al tipo 'System.Windows.Controls.ListViewItem'." Source="GesHoras" StackTrace: en GesHoras.Classes.cBackgroundConvertor.Convert(Object value, Type targetType, Object parameter, CultureInfo culture) en E:\Proyectos\VS2008\Mañas\GesHorasApp\Classes\cBackgroundConvertor.cs:línea 17 en System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange) en System.Windows.Data.BindingExpression.Activate(Object item) en System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt) en System.Windows.Data.BindingExpression.AttachOverride(DependencyObject target, DependencyProperty dp) en System.Windows.Data.BindingExpressionBase.Attach(DependencyObject target, DependencyProperty dp) en System.Windows.Data.BindingExpressionBase.OnAttach(DependencyObject d, DependencyProperty dp) en System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, OperationType operationType, Boolean isInternal) en System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value) en System.Windows.TemplateApplicationHelper.SetDependencyValueCore(DependencyObject dependencyObject, DependencyProperty dp, Object value) en System.Windows.Markup.BamlRecordReader.SetDependencyValue(DependencyObject dependencyObject, DependencyProperty dependencyProperty, Object value) en System.Windows.Markup.BamlRecordReader.SetDependencyComplexProperty(Object currentTarget, BamlAttributeInfoRecord attribInfo, Object o) InnerException:
I know there are other maners to alternate background for listview items but they don't work when grouping.
Thanks in advance!