.net - 整个 WPF ListView 中的内部网格线
问题描述
我需要在我的经典 Windows WPF 应用程序中实现以下设计(预期):
我使用 WPF ListView 控件,并且已经实现了许多需要的功能,但我无法完全实现设计师的愿景。目前我的控件看起来像以下架构(实际):
它有两个缺点:
- 网格线放置在单元格的内部和外部,但初始样式仅提供内部线。
- 网格线仅在填充行上延伸,但我需要填充所有控制区域,甚至是空白(未使用)空间。
谁能帮我解决这些问题?也许我需要使用另一个 WPF 控件?
谢谢。
解决方案
我不确定是否ListView
适合Control
您的条件。也许GridView
或 aDataGrid
是一个更好的决定。
这是和的默认值的修改styles
版本。获取片段的这一部分并根据您的需要对其进行扩展。ListView
ListViewItem
风格
<SolidColorBrush x:Key="ListBorder" Color="#828790"/>
<Style x:Key="ListViewStyle1" TargetType="{x:Type ListView}">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="BorderBrush" Value="{StaticResource ListBorder}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListView}">
<Border x:Name="Bd" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="1" SnapsToDevicePixels="true">
<ScrollViewer Focusable="false" Padding="{TemplateBinding Padding}">
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ScrollViewer>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
</Trigger>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="BorderBrush" Value="Green"/>
<Setter Property="BorderThickness" Value="0,0,1,1"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Padding" Value="2,0,0,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true"/>
<Condition Property="Selector.IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
变化:
<Setter Property="BorderBrush" Value="Green"/>
<Setter Property="BorderThickness" Value="0,0,1,1"/>
XAML
<ListView VerticalAlignment="Center" HorizontalAlignment="Center" Style="{DynamicResource ListViewStyle1}">
<ListViewItem>
<TextBlock Text="Hello World"/>
</ListViewItem>
<ListViewItem>
<TextBlock Text="Hello World"/>
</ListViewItem>
<ListViewItem>
<TextBlock Text="Hello World"/>
</ListViewItem>
</ListView>
预习
推荐阅读
- python - 如何获取两个日期之间的天数?
- r - 合并两个数据集但没有重复
- selenium - groovy.lang.MissingPropertyException:没有这样的属性:类工具包:custom.SampleKeyword
- awk - 文件行的信誉变化
- qt - 树视图中自定义数据的 Mime 类型
- android-studio - gradle在Android Studio中成功同步时如何隐藏/折叠构建窗口
- node.js - 生产服务器上的nodejs 500内部服务器错误
- java - RegEx 用于替换字符串中的数字
- java - 如何仅将特殊字符转换为 html 实体而不转义 <、>、" 和 '?
- github - 在 GitHub 拉取请求检查选项卡中查看检查状态