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

发布于 2024-09-06 16:30:26 字数 1378 浏览 2 评论 0原文

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

从自定义面板开始,沿着树向上走,我得到以下祖先:

  • ItemsPresenter: 15x15
  • ScrollViewer: 15x15
  • Grid: 15x15
  • ScrollViewer: 15x15
  • Border: 17x17
  • ListView: 17x17

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

我希望避免重新设计整个 ListView。其他一切都工作正常。有什么方法可以删除这个像素边框,也许通过一种样式?

I have a ListView that goes something like this:

<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>

When it's empty, the Width and Height of the custom panel I've defined is 15 x 15. I can confirm these are the actual dimensions at runtime. However, the ListView itself has dimensions of 17 x 17 (that is, a one pixel border between the panel and the ListView).

Starting from the custom panel and walking up the tree, I get the following ancestors:

  • ItemsPresenter: 15x15
  • ScrollViewer: 15x15
  • Grid: 15x15
  • ScrollViewer: 15x15
  • Border: 17x17
  • ListView: 17x17

If I change the Padding on the ListView to -1, it removes the border but causes several other issues.

I'm hoping to avoid retemplating the entire ListView. Everything else is working fine. Is there some way I can Remove this one pixel border, perhaps through a style?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南笙 2024-09-13 16:30:26

这非常简单,如果您有混合,您可以右键单击列表视图并选择编辑模板并从边框中删除填充。您可以直接在 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}"  />

It is really simple, if you have blend you can right click the listview and choose to edit the template and remove the padding from the border. You can do this directly in xaml. Since I want all of my listviews not to have this border, I create a resource file called MyprojectnameResources.xaml in the root of the project with this content.

<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. -->

Then you can simply use this template with any listview you want

<ListView Width="1020" Height="764" ItemsSource="{Binding Destinations}" ItemTemplate="{DynamicResource DestinationDataTemplate}"  Canvas.Top="0" Canvas.Left="0" BorderThickness="0" Template="{DynamicResource ListViewNewTemplate}"  />
私野 2024-09-13 16:30:26

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

<ListView Padding="-1" />

A quick and dirty solution (emphasis on "dirty"):

<ListView Padding="-1" />
雅心素梦 2024-09-13 16:30:26

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

<ListBox BorderThickness="0" />

不需要模板...

The simplest solution for me was to set the Border's thickness to 0, like:

<ListBox BorderThickness="0" />

No templates required...

忘羡 2024-09-13 16:30:26

我刚刚查看了 ListView 的默认模板,确实 Border 的显式填充为 1...所以我认为您无法在不重新定义的情况下做到这一点模板。无论如何,这并不是很困难:只需使用 StyleSnooper 或 ShowMeTheTemplate 之类的工具复制默认模板(我认为 Blend 也可以做到),然后更改您需要的内容...

I just had a look at the default template for ListView, indeed the Border has an explicit padding of 1... so I don't think you can do it without redefining the template. Anyway, it's not very difficult : just copy the default template using a tool like StyleSnooper or ShowMeTheTemplate (I think Blend can do it too), and change what you need...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文