Reputation: 173
I have a list view as following:
<ListView x:Name="lvLedger"
Height="{Binding Path=GridHight, ElementName=ledgerList}"
Width="{Binding Path=GridWidth, ElementName=ledgerList}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding}"
BorderThickness="0"
Background="Transparent"
BorderBrush="Transparent"
DataContextChanged="lvLedger_DataContextChanged">
<ListView.View>
<GridView>
<GridViewColumn x:Name="c2ServiceDate" Header="Service Date" Width="82" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=servicedate}"
ToolTipService.ShowDuration="60000"
ToolTipService.InitialShowDelay="0"
ToolTip="{Binding Path=type}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn x:Name="c3CPT" Header="Code" Width="50">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=cpt}"
ToolTipService.ShowDuration="60000"
ToolTipService.InitialShowDelay="0"
ToolTip="{Binding Path=type}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
<!--More columns here--> </GridViewColumn></GridView></ListView.View></ListView>
What i would like to be able to do is change the background color of the the row based on the combination of Service Date and Code. So i may have 3 lines in a row with the same service date and code which should have the same background followed by 2 lines with a different color and then alternate based on the same rule
1/19/11 356 (blue)
1/19/11 356 (blue)
1/19/11 235 (red)
2/20/11 356 (blue)
2/20/11 356 (blue)
2/20/11 356 (blue)
2/21/11 564 (red)
2/21/11 564 (red)
2/21/11 564 (red)
2/21/11 564 (red)
2/25/11 798 (blue)
and soo on...
ItemSource is being bounded to a DataView from an external control.
I really have no idea how i could do something like that and any help would be appriciated.
Upvotes: 3
Views: 13979
Reputation: 6316
How about adding a ColorProperty to the class/model that your row(line) is bound to. Then there you already have dates and numbers there. Once you set them, set the color as well, now without converters, you can just trigger on that property:
<Style x:Key="ListViewItemStyle" TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border x:Name="Border" SnapsToDevicePixels="true">
<GridViewRowPresenter VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
<Border.Style>
<Style TargetType="Border">
<Style.Triggers>
<DataTrigger Binding="{Binding ColorProperty}" Value="Blue">
<Setter Property="Background" Value="Blue"/>
</DataTrigger>
<DataTrigger Binding="{Binding ColorProperty}" Value="Red">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Upvotes: 2