改变的GridView行背景颜色在ListView [英] Change background color of GridView row in a ListView

查看:326
本文介绍了改变的GridView行背景颜色在ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图如下:

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>

我希望能够做的就是改变基于服务日期和code的组合行的背景颜色。所以我可能有3条线路的行具有相同服务日期和code里面应该有相同的背景其次是2线用不同的颜色,然后基于相同的规则交替

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

11年1月19日356(蓝色)

11年1月19日356(蓝色)

11年1月19日235(红)

11年2月20日356(蓝色)

11年2月20日356(蓝色)

11年2月20日356(蓝色)

11年2月21日564(红)

11年2月21日564(红)

11年2月21日564(红)

11年2月21日564(红)

11年2月25日798(蓝色)

和洙...

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被限定到DataView从外部的控制

ItemSource is being bounded to a DataView from an external control.

我真的不知道我怎么会做这样的事情和任何帮助将appriciated。

I really have no idea how i could do something like that and any help would be appriciated.

推荐答案

如何添加ColorProperty到你的行(线)绑定到类/模式。再就是你已经有了日期和数字在那里。一旦您设置,设置颜色为好,现在不用转换器,你可以触发该属性:

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> 

这篇关于改变的GridView行背景颜色在ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆