Change background color of GridView row in a ListView

本小妞迷上赌 提交于 2019-12-13 15:14:14

问题


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.


回答1:


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> 


来源:https://stackoverflow.com/questions/9999691/change-background-color-of-gridview-row-in-a-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!