Изменить цвет фона строки GridView в ListView - PullRequest
2 голосов
/ 03 апреля 2012

У меня есть список в следующем виде:

<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>

То, что я хотел бы сделать, это изменить цвет фона строки на основе комбинации даты обслуживания и кода. Таким образом, у меня может быть 3 строки подряд с одинаковой датой обслуживания и кодом, которые должны иметь один и тот же фон, за которыми следуют 2 строки другого цвета, а затем чередоваться в соответствии с тем же правилом

1/19/11 356 (синий)
1/19/11 356 (синий)
1/19/11 235 (красный)
20.02.11 356 (синий)
20.02.11 356 (синий)
20.02.11 356 (синий)
21.02.11 564 (красный)
21.02.11 564 (красный)
21.02.11 564 (красный)
21.02.11 564 (красный)
25.02.11 798 (синий)
и так на ...

ItemSource привязан к DataView из внешнего элемента управления.

Я действительно понятия не имею, как я могу сделать что-то подобное, и любая помощь будет оценена.

1 Ответ

2 голосов
/ 03 апреля 2012

Как насчет добавления ColorProperty к классу / модели, с которой связана ваша строка (строка). Тогда там у вас уже есть даты и цифры. Как только вы установите их, установите цвет, теперь без конвертеров вы можете просто активировать это свойство:

<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> 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...