我正在 WPF 中设置项目的样式ListBox
,并希望在每个项目周围放置边框。和BorderThickness
设置为 1,例如,相邻项目之间的上下边框都会被绘制,因此看起来比侧边框“更厚”,如下所示:
生成这些的项目模板ListBoxItems
is:
<DataTemplate>
<Border BorderThickness="1" BorderBrush="DarkSlateGray" Background="DimGray" Padding="8 4 8 4">
<TextBlock Text="{Binding Name}" FontSize="16"/>
</Border>
</DataTemplate>
我想“折叠”这些相邻的边界,例如,通过CSS http://htmldog.com/reference/cssproperties/border-collapse/。我知道BorderThickness
可以单独定义左/右/上/下边框,但这也会影响第一个或最后一个项目的边框,这是不希望的。
有没有办法用 WPF 来实现这一点?的财产Border
我失踪了,还是需要采用不同的方法来创建边界?
Use BorderThickness="1,0,1,1"
and a DataTrigger
它检查RelativeSource={RelativeSource Mode=PreviousData}
being null
to set BorderThickness="1,1,1,1"
:
<Window x:Class="CollapseBordersDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="239" Width="525">
<Window.Resources>
<x:Array x:Key="ListBoxItems" Type="{x:Type sys:String}">
<sys:String>Alice</sys:String>
<sys:String>Bob</sys:String>
<sys:String>Colleen</sys:String>
</x:Array>
<DataTemplate x:Key="ListBoxTemplate">
<Border x:Name="Border" BorderThickness="1,0,1,1" BorderBrush="DarkSlateGray" Background="LightGray" Padding="8 4 8 4">
<TextBlock Text="{Binding}" FontSize="16"/>
</Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=PreviousData}}" Value="{x:Null}">
<Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,1"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{StaticResource ListBoxItems}" ItemTemplate="{StaticResource ListBoxTemplate}" HorizontalContentAlignment="Stretch" />
</Grid>
</Window>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)