Wpf 合并列表框中包含的项的边框
我有一个列表框,其中UniformGrid作为它的ItemsPanel。基本上,我不想将项目显示为带边框的矩形。我使用UniformGrid作为ItemsPanel,在这里我可以控制通过绑定显示的行和列的数量 我使用ListBox的ItemContainerStyle为每个项目设置边框。我可以指定边框厚度,它确实会出现在列表中的所有项目周围。问题是相邻项目的边框不合并,从而为相邻项目提供了一个“双边框”。如何控制每个项目的边框,使每个项目具有唯一的厚度,即使它可能有相邻的项目 这是按比例缩小的代码Wpf 合并列表框中包含的项的边框,wpf,wpf-4.0,Wpf,Wpf 4.0,我有一个列表框,其中UniformGrid作为它的ItemsPanel。基本上,我不想将项目显示为带边框的矩形。我使用UniformGrid作为ItemsPanel,在这里我可以控制通过绑定显示的行和列的数量 我使用ListBox的ItemContainerStyle为每个项目设置边框。我可以指定边框厚度,它确实会出现在列表中的所有项目周围。问题是相邻项目的边框不合并,从而为相邻项目提供了一个“双边框”。如何控制每个项目的边框,使每个项目具有唯一的厚度,即使它可能有相邻的项目 这是按比例缩小的代
<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.将网格边距设置为
。然后在左侧和顶部有额外的1px,即项目边框厚度。2.将项目边距设置为
。更正确或更低沉的项目将与其邻居一致。最左边和最上面的项目填充了1倍的网格边距。我遇到了与TabControl/TabItem相同的问题,但问题是相同的。我通过重新模板TabItem(在您的例子中是ListBoxItem)添加一个触发器来改变边框(注意第一个ControlTemplateTrigger)来解决这个问题
为了让它工作,你必须让你的第一件东西有点不同。。。我选择将我的第一个项目的标签设置为“first”,以便触发触发器。(TabControl.Item[0]。填充TabControl后标记=“第一个”)
可以是,
用于项目,并为整个列表框设置左边框和上边框?使用Value=“0,0,1,1”,相邻单元格的边框厚度正确,但上行单元格的上边框松动,最左列单元格的左边框松动!尝试用边框包装UniformGrid,这是BorderThickness=“1,1,0,0”
。我尝试过,但ItemsPanelTemplate不允许这样做。它只接受作为面板的元素。更改网格的边距:
。为项目保留边框:
。为项目添加负边距:
。
<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>