带有垂直网格线和背景颜色的ListView

时间:2012-05-07 16:32:11

标签: wpf listview gridlines

我有一个使用以下样式的垂直线的WPF ListView :(对不起长度,但我认为我应该包括它)

    <SolidColorBrush x:Key="verticalLineColor" Color="Red" />
    <Style x:Key="{x:Static GridView.GridViewScrollViewerStyleKey}" TargetType="{x:Type ScrollViewer}">
        <Setter Property="Focusable" Value="false"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ScrollViewer}">
                    <Grid SnapsToDevicePixels="true" Background="{TemplateBinding Background}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <DockPanel Margin="{TemplateBinding Padding}">
                            <ScrollViewer Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" DockPanel.Dock="Top">
                                <GridViewHeaderRowPresenter x:Name="viewHeaderRowPresenter"
                                        Margin="-2,0,-2,0"
                                        Columns="{Binding Path=TemplatedParent.View.Columns, RelativeSource={RelativeSource TemplatedParent}}"
                                        ColumnHeaderContainerStyle="{Binding Path=TemplatedParent.View.ColumnHeaderContainerStyle, RelativeSource={RelativeSource TemplatedParent}}"
                                        ColumnHeaderTemplate="{Binding Path=TemplatedParent.View.ColumnHeaderTemplate, RelativeSource={RelativeSource TemplatedParent}}"
                                        ColumnHeaderTemplateSelector="{Binding Path=TemplatedParent.View.ColumnHeaderTemplateSelector, RelativeSource={RelativeSource TemplatedParent}}"
                                        AllowsColumnReorder="{Binding Path=TemplatedParent.View.AllowsColumnReorder, RelativeSource={RelativeSource TemplatedParent}}"
                                        ColumnHeaderContextMenu="{Binding Path=TemplatedParent.View.ColumnHeaderContextMenu, RelativeSource={RelativeSource TemplatedParent}}"
                                        ColumnHeaderToolTip="{Binding Path=TemplatedParent.View.ColumnHeaderToolTip, RelativeSource={RelativeSource TemplatedParent}}"
                                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />


                            </ScrollViewer>

                            <ScrollContentPresenter x:Name="PART_ScrollContentPresenter"
                                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"                                         
                                        ContentTemplate="{TemplateBinding ContentTemplate}"
                                        KeyboardNavigation.DirectionalNavigation="Local"
                                        CanContentScroll="{TemplateBinding CanContentScroll}">

                                <ScrollContentPresenter.Content>
                                    <Grid>
                                        <!-- Container of vertical and horizontal lines -->
                                        <ItemsControl Margin="-1,0,0,0" ItemsSource="{Binding Path=TemplatedParent.View.Columns, RelativeSource={RelativeSource TemplatedParent}}">
                                            <ItemsControl.ItemTemplate>
                                                <DataTemplate>
                                                    <Border Width="{Binding Path=ActualWidth}" BorderThickness="0,0,.5,0" BorderBrush="{DynamicResource verticalLineColor}" Opacity="1" />
                                                </DataTemplate>
                                            </ItemsControl.ItemTemplate>
                                            <ItemsControl.ItemsPanel>
                                                <ItemsPanelTemplate>
                                                    <StackPanel Orientation="Horizontal" />
                                                </ItemsPanelTemplate>
                                            </ItemsControl.ItemsPanel>
                                        </ItemsControl>
                                        <ContentControl Content="{TemplateBinding Content}" />
                                    </Grid>
                                </ScrollContentPresenter.Content>
                            </ScrollContentPresenter>
                        </DockPanel>
                        <ScrollBar Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Cursor="Arrow" x:Name="PART_HorizontalScrollBar" Grid.Row="1" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0.0" Value="{Binding Path=HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" Orientation="Horizontal" ViewportSize="{TemplateBinding ViewportWidth}"/>
                        <ScrollBar Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Cursor="Arrow" x:Name="PART_VerticalScrollBar" Grid.Column="1" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0.0" Value="{Binding Path=VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" Orientation="Vertical" ViewportSize="{TemplateBinding ViewportHeight}"/>
                        <DockPanel Grid.Column="1" Grid.Row="1" Background="{Binding Path=Background, ElementName=PART_VerticalScrollBar}" LastChildFill="false">
                            <Rectangle Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Width="1" Fill="White" DockPanel.Dock="Left"/>
                            <Rectangle Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Height="1" Fill="White" DockPanel.Dock="Top"/>
                        </DockPanel>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

现在,这里的一切都很棒。我现在遇到的问题是,如果我的一个字段为0,我需要对其中一个ListView行进行着色。我有以下代码可以让我90%的方式:

        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="VerticalContentAlignment" Value="Top" />
                <Setter Property="Foreground" Value="Black"/>
                <Setter Property="Margin" Value="0,-2,0,-1"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding QtyInBox}" Value="0">
                        <Setter Property="Background" Value="LightGray"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListView.ItemContainerStyle>

现在,我遇到的问题是,当我设置背景时,它似乎“覆盖”网格线。 enter image description here

有没有办法让这些垂直线显示出来?

编辑#1:好的,所以在玩了一会儿之后,我想知道这是否可行;在以下代码部分中:

<DataTemplate>
    <Border Width="{Binding Path=ActualWidth}" BorderThickness="0,0,.5,0" BorderBrush="{DynamicResource verticalLineColor}" />
</DataTemplate>

如果我能以某种方式挂钩那里的Background属性,我应该能够让它工作。有人能指出我在这里绑定的正确方向吗?我需要绑定的字段是ListView的QtyInBox上的ItemSource

编辑#2:看起来我错了;这似乎是整个专栏的约束力;所以我不能在那里设置一行的背景。回到绘图板......

1 个答案:

答案 0 :(得分:0)

不幸的是,我无法找到真正的解决方案。

我能做的最好的事情是将背景的透明度设置为.6或.7。

不是一个完美的解决方案;因为它导致文本有点模糊,但现在已经足够好了。