2010-06-20 21 views
7

我有一个ListView即是这样的:WPF ListView在内部布局面板周围有一个像素边框。我如何摆脱它?

<ListView 
    x:Name="SeriesListView" 
    SnapsToDevicePixels="True" 
    ItemsSource="{Binding Items}" 
    BorderBrush="Transparent" BorderThickness="0" 
    Padding="0" Margin="0" 
    VerticalContentAlignment="Top" 
    Background="Purple" 
    LostFocus="ListView_LostFocus" 
    > 

    <ListView.ItemsPanel> 
     <ItemsPanelTemplate> 
      <local:LDSeriesPanel 
       SnapsToDevicePixels="True" 
       MaxWidth="{Binding ElementName=itControl,Path=ActualWidth}" 
       VerticalAlignment="Stretch" HorizontalAlignment="Stretch" 
       MinHeight="{x:Static local:LDSeriesPanel.MIN_HEIGHT}" 
       MinWidth="{x:Static local:LDSeriesPanel.MIN_WIDTH}" 
       Margin="0" 
       Background="Aquamarine"/> 
     </ItemsPanelTemplate> 
    </ListView.ItemsPanel> 
</ListView> 

当它是空的,宽度和我定义的自定义面板的高度是15×15,我可以证实,这些都是在运行时的实际尺寸。但是,ListView本身的尺寸为17 x 17(即面板和ListView之间的一个像素边框)。

从自定义面板启动和走了树,我碰到下面的祖先:

  • ItemsPresenter:15×15
  • 的ScrollViewer:15×15
  • 格:15×15
  • 的ScrollViewer:15×15
  • 边框:17x17
  • 列表视图:17x17

如果我将ListView上的填充更改为-1,它将删除边框但会导致其他几个问题。

我希望避免retemplating整个ListView。其他一切工作正常。有什么方法可以删除这一个像素边框,也许是通过一种风格?

回答

2

我只是看看ListView的默认模板,的确Border的显式填充为1 ......所以我不认为如果不重新定义模板就可以做到这一点。无论如何,这不是非常困难:只需使用像StyleSnooper或ShowMeTheTemplate(我认为Blend也可以这样做)来复制默认模板,并改变你需要的东西...

+0

哎呀....确定。 :( – 2010-06-21 02:10:24

+1

我最终使用了一个ListBox来代替,但我会给你勾号的。这个问题的竞争很激烈......) – 2010-06-23 16:18:48

8

这很简单,如果你有混合您可以右键单击列表视图并选择编辑模板并从边框中删除填充。您可以直接在xaml中执行此操作。由于我希望我所有的列表视图都没有这个边框,所以我在该项目的根目录下创建了一个名为MyprojectnameResources.xaml的资源文件。

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
<ControlTemplate x:Key="ListViewNewTemplate" TargetType="{x:Type ListBox}"> 
    <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="0" 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> 
<!-- Resource dictionary entries should be defined here. --> 

然后,你可以简单地使用这个模板与任何列表视图你想

<ListView Width="1020" Height="764" ItemsSource="{Binding Destinations}" ItemTemplate="{DynamicResource DestinationDataTemplate}" Canvas.Top="0" Canvas.Left="0" BorderThickness="0" Template="{DynamicResource ListViewNewTemplate}" /> 
+0

这是边界上愚蠢的Padding =“1”。将其更改为Padding =“0”是解决方案。 – DaleyKD 2016-05-02 14:58:19

5

一个快速和肮脏的解决方案(强调 “脏”):

<ListView Padding="-1" /> 
+0

谢谢!在WinRT中完美工作。 – 2016-02-03 01:25:50

3

对我来说最简单的解决方案是将边框的厚度设置为0,如:

<ListBox BorderThickness="0" /> 

无需模板...

相关问题