[英]WPF ListView has a one pixel border around the internal layout panel. How do I get rid of it?
我有一個這樣的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 x15。我可以確認它們是運行時的實際尺寸。 但是,ListView本身的尺寸為17 x 17(即面板和ListView之間的一個像素邊框)。
從定制面板開始,然后沿着樹走,我得到了以下祖先:
如果我將ListView上的Padding更改為-1,它將刪除邊框,但會導致其他一些問題。
我希望避免重新模板化整個ListView。 其他一切工作正常。 有什么方法可以刪除一個像素邊框,可能是通過樣式嗎?
這非常簡單,如果您有混合功能,則可以右鍵單擊列表視圖,然后選擇編輯模板並從邊框中刪除填充。 您可以直接在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}" />
快速而骯臟的解決方案(強調“骯臟”):
<ListView Padding="-1" />
對我來說,最簡單的解決方案是將Border的厚度設置為0,例如:
<ListBox BorderThickness="0" />
無需模板...
我只是看了ListView
的默認模板,實際上Border
的顯式填充為1 ...,所以我認為不重新定義模板就可以做到這一點。 無論如何,這並不困難:只需使用StyleSnooper或ShowMeTheTemplate之類的工具(我認為Blend也可以做到)復制默認模板,然后更改所需的內容即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.