WPF ListView 在内部布局面板周围有一个一像素边框。我该如何摆脱它?
我有一个类似这样的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这非常简单,如果您有混合,您可以右键单击列表视图并选择编辑模板并从边框中删除填充。您可以直接在 xaml 中执行此操作。由于我希望所有列表视图都没有此边框,因此我在项目的根目录中创建了一个名为 MyprojectnameResources.xaml 且包含此内容的资源文件。
然后您可以简单地将此模板与您想要的任何列表视图一起使用
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.
Then you can simply use this template with any listview you want
一个快速而肮脏的解决方案(强调“肮脏”):
A quick and dirty solution (emphasis on "dirty"):
对我来说最简单的解决方案是将边框的厚度设置为 0,例如:
不需要模板...
The simplest solution for me was to set the Border's thickness to 0, like:
No templates required...
我刚刚查看了
ListView
的默认模板,确实Border
的显式填充为 1...所以我认为您无法在不重新定义的情况下做到这一点模板。无论如何,这并不是很困难:只需使用 StyleSnooper 或 ShowMeTheTemplate 之类的工具复制默认模板(我认为 Blend 也可以做到),然后更改您需要的内容...I just had a look at the default template for
ListView
, indeed theBorder
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...