代码之家  ›  专栏  ›  技术社区  ›  Scott Whitlock

WPF ListView在内部布局面板周围有一个单像素边框。我怎么摆脱它?

  •  7
  • Scott Whitlock  · 技术社区  · 14 年前

    我有一个这样的列表视图:

    <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 x 15。我可以确认这些是运行时的实际尺寸。但是,ListView本身的尺寸为17 x 17(即面板和ListView之间的一个像素边界)。

    从定制面板开始,走到树上,我得到了以下祖先:

    • 项目预设:15x15
    • 滚动查看器:15x15
    • 网格:15x15
    • 滚动查看器:15x15
    • 边境:17X17
    • ListVIEW:17X17

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

    我希望避免重新测试整个ListView。其他一切都很好。有没有什么方法可以删除这个单像素边框,也许是通过样式?

    4 回复  |  直到 9 年前
        1
  •  2
  •   Thomas Levesque    14 年前

    我刚刚查看了默认模板 ListView 事实上 Border 显式填充为1…所以我认为你不需要重新定义模板就不能做到这一点。不管怎样,这并不难:只需使用StylesNoOper或ShowMeteTemplate之类的工具复制默认模板(我认为Blend也可以这样做),然后更改您需要的内容…

        2
  •  9
  •   Alkampfer    14 年前

    这非常简单,如果有Blend,可以右键单击ListView并选择编辑模板并从边框中删除填充。您可以在XAML中直接执行此操作。由于我希望所有的ListView都不具有此边框,因此在包含此内容的项目根目录中创建一个名为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一起使用。

    <ListView Width="1020" Height="764" ItemsSource="{Binding Destinations}" ItemTemplate="{DynamicResource DestinationDataTemplate}"  Canvas.Top="0" Canvas.Left="0" BorderThickness="0" Template="{DynamicResource ListViewNewTemplate}"  />
    
        3
  •  5
  •   theartwebreathe    10 年前

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

    <ListView Padding="-1" />
    
        4
  •  4
  •   raider33    9 年前

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

    <ListBox BorderThickness="0" />
    

    不需要模板…