2010-06-20 22 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" /> 

無需模板...

相關問題