合并包含在列表框中的项目的边框
问题描述:
我有一个带有UniformGrid的ListBox作为其ItemsPanel。基本上,我不想将项目显示为带有边框的矩形。我使用UniformGrid作为ItemsPanel,我可以控制通过绑定显示的行数和列数。合并包含在列表框中的项目的边框
我正在使用ListBox的ItemContainerStyle设置每个项目的边框。我可以指定BorderThickness,它确实出现在列表中的所有项目周围。问题是,边界不合并为给予“双边框”对相邻的项目相邻的项目。我如何控制每件商品的边框,使每件商品都有独特的厚度,即使它们可能有相邻的商品。
这里是缩小代码
<ListBox x:Name="lstGroups" ItemsSource="{Binding Groups}" Grid.Row="1" Style="{StaticResource RelayDispositionStyle}"
SelectedItem="{Binding SelectedGroup}" gs:DragDropExtension.ScrollOnDragDrop="True"
ItemContainerStyle="{StaticResource ItemContainerStyle}"
>
</ListBox>
<Style x:Key="RelayDispositionStyle" TargetType="{x:Type ListBox}" BasedOn="{StaticResource {x:Type ListBox}}">
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<UniformGrid Rows="{Binding ElementName=Racks, Path=SelectedItem.NoOfRows}"
Columns="{Binding ElementName=Racks, Path=SelectedItem.GroupsPerRow}"
HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,10,0,0">
</UniformGrid>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="RelayDispositionItemContainerStyle" TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="MidnightBlue"/>
</Style>
答
您可以使用切缘阴性一招:
1.设置网格利润率<UniformGrid Margin="1,11,0,0">
。然后左边和顶部还有1px,这是项目边框的粗细。
2.将项目边距设置为<Setter Property="Margin" Value="-1,-1,0,0"/>
。更正确或更偏向于的项目将与邻居重合。最左边和最上面的项目填充1px的网格边距。
答
我碰到与TabControl/TabItem相同的问题,但问题是相同的。我通过retemplating的TabItem的解决这个问题(在你的情况下,它会是一个ListBoxItem)将触发改变边界(首先注意到的ControlTemplateTrigger)。
为了使其工作,你必须让你的第一个项目不知何故diffrent ...我选择将我的第一个项目的标签设置为“第一”以触发触发器。 (TabControl.Item [0] = .TAG “第一” 填充的TabControl之后)
<Style x:Key="styleTabItemMetro" TargetType="{x:Type TabItem}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Foreground" Value="Green" />
<Setter Property="Margin" Value="0 0 0 0" />
<Setter Property="Padding" Value="5 2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid Margin="{TemplateBinding Margin}"
Background="{TemplateBinding Background}">
<Border Margin="0 4"
Name="borderBorder"
BorderBrush="#FF8C8E94" BorderThickness="1 0 0 0" CornerRadius="0" />
<Grid Cursor="Hand" Margin="{TemplateBinding Padding}">
<ContentPresenter x:Name="ContentSite"
ContentSource="Header"
Margin="12,2"
RecognizesAccessKey="True">
<ContentPresenter.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TabItem}}, Path=Foreground}" />
</Style>
</ContentPresenter.Resources>
</ContentPresenter>
</Grid>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Path=Tag, RelativeSource={RelativeSource Mode=Self}}" Value="First">
<Setter TargetName="borderBorder" Property="BorderThickness" Value="0"/>
</DataTrigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#FFF9F9F9" />
<Setter Property="Padding" Value="0 1 0 0" />
<Setter Property="Margin" Value="0 0 0 0" />
<Setter Property="Panel.ZIndex" Value="100" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Green" />
<Setter Property="Foreground" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{StaticResource ResourceKey=brushHeaderBackground}" />
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
可能,''为项目并为整个列表框设置左侧和顶部边框? –
Vladimir
2013-04-29 12:17:30
随着值=“0,0,1,1”,相邻的单元具有正确边界的厚度,但在顶行中的细胞失去它们的上缘,并在最左边的列中的单元失去其左边界! – Jatin 2013-04-29 12:22:38
尝试换用UniformGrid边界,这'了borderThickness = “1,1,0,0”'。 – Vladimir 2013-04-29 12:32:37