在Windows 8 Metro'ListView'中无法消除Tiles之间的填充/边距

iTr*_*out 9 c# xaml listview padding windows-8

我正在尝试ListView在a中构建两个对象StackPanel并使所有ItemTemplateTiles彼此"接触"(意味着ListViews 内没有边距或填充).似乎Windows 8 Metro具有某种内置填充/边距.我的问题:如何删除这些或将它们设置为0

这是我的代码:

<StackPanel x:Name="teesSP" 
            HorizontalAlignment="Left" 
            Orientation="Horizontal" 
            VerticalAlignment="Top" >

    <ListView x:Name="timesLV1"            
              SelectionMode="Multiple" 
              SelectionChanged="timesLV_Click" 
              ItemTemplate="{StaticResource TimeTileTemplate}">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="Padding" Value="0"/>
                <Setter Property="Margin" Value="0"/>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>

    <ListView x:Name="timesLV2"            
              SelectionMode="Multiple" 
              SelectionChanged="timesLV_Click" 
              ItemTemplate="{StaticResource TimeTileTemplate}">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="Padding" Value="0"/>
                <Setter Property="Margin" Value="0"/>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>

</StackPanel>
Run Code Online (Sandbox Code Playgroud)

ItemTemplate是:

<DataTemplate x:Key="TimeTileTemplate">
    <Grid HorizontalAlignment="Center" Background="White" >
        <Border BorderBrush="Black" BorderThickness="2" >
            <StackPanel Margin="0,0,0,0" Orientation="Horizontal"
                Width="130" Height="60" VerticalAlignment="Center" >
                <TextBlock Margin="2,0,0,0" TextWrapping="Wrap"
                    Style="{StaticResource ItemSubtitleStyle}"
                    VerticalAlignment="Center"
                    HorizontalAlignment="Left"
                    Text="{Binding startTime}" Width="70" />
                <TextBlock TextWrapping="Wrap"
                    Style="{StaticResource ItemTitleStyle}"
                    VerticalAlignment="Center"
                    HorizontalAlignment="Right"
                    Text="{Binding startHole}" Width="40" />
            </StackPanel>
        </Border>
    </Grid>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

......它呈现以下内容:

带边距/填充的ListView

小智 8

你应该提供负面的Margin:

<ListView.ItemContainerStyle>
   <Style TargetType="ListViewItem">
      <Setter Property="HorizontalContentAlignment" Value="Left" />
      <Setter Property="VerticalContentAlignment" Value="Center" />
      <Setter Property="Margin" Value="0,0,0,-8" />
   </Style>
</ListView.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)

  • 使用负边距玩是一种黑客攻击.这是可以想象的最不受支持的代码. (5认同)

T.D*_*uis 6

经过1小时的搜索.这是比负余量更好的解决方案:

以下,代码允许您拥有您想要的保证金!(这里:0)

<Style x:Key="ListViewItemStyle1" TargetType="ListViewItem">
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="ListViewItem">
            <ListViewItemPresenter ContentMargin="0" />
          </ControlTemplate>
      </Setter.Value>
    </Setter>            
</Style>
Run Code Online (Sandbox Code Playgroud)

...

<ListView ItemContainerStyle="{StaticResource ListViewItemStyle1}">
 //your listView items
</ListView>
Run Code Online (Sandbox Code Playgroud)


Cat*_*Cap 5

我已经为这个问题找到了相当清晰的解决方案!

<Style x:Key="NoSpacesListViewItemStyle" TargetType="ListViewItem">
    <Setter Property="Margin" Value="0"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListViewItem">
                <ListViewItemPresenter ContentMargin="0" SelectionCheckMarkVisualEnabled="False" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<ListView     IsSwipeEnabled="False"
              ItemContainerStyle="{StaticResource NoSpacesListViewItemStyle}"
              ItemTemplate="{StaticResource SomeTemplate}"
              ItemsSource="{Binding SomeData}"
              SelectionMode="None"/>
Run Code Online (Sandbox Code Playgroud)

此外,我可以承认选择边框在这种情况下不起作用.因此,此方法不适合具有选择的ListView.

有完整的默认ListViewItemStyle具有相同的更改:

<Style x:Key="NoSpacesListViewItemStyle" TargetType="ListViewItem">
    <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
    <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="TabNavigation" Value="Local" />
    <Setter Property="IsHoldingEnabled" Value="True" />
    <Setter Property="Margin" Value="0" />
    <Setter Property="HorizontalContentAlignment" Value="Left" />
    <Setter Property="VerticalContentAlignment" Value="Top" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListViewItem">
                <ListViewItemPresenter HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                       VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                       CheckBrush="{ThemeResource ListViewItemCheckThemeBrush}"
                                       CheckHintBrush="{ThemeResource ListViewItemCheckHintThemeBrush}"
                                       CheckSelectingBrush="{ThemeResource ListViewItemCheckSelectingThemeBrush}"
                                       ContentMargin="0"
                                       ContentTransitions="{TemplateBinding ContentTransitions}"
                                       DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}"
                                       DragBackground="{ThemeResource ListViewItemDragBackgroundThemeBrush}"
                                       DragForeground="{ThemeResource ListViewItemDragForegroundThemeBrush}"
                                       DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}"
                                       FocusBorderBrush="{ThemeResource ListViewItemFocusBorderThemeBrush}"
                                       Padding="{TemplateBinding Padding}"
                                       PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}"
                                       PointerOverBackground="{ThemeResource ListViewItemPointerOverBackgroundThemeBrush}"
                                       PointerOverBackgroundMargin="1"
                                       ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}"
                                       SelectedBackground="{ThemeResource ListViewItemSelectedBackgroundThemeBrush}"
                                       SelectedBorderThickness="{ThemeResource ListViewItemCompactSelectedBorderThemeThickness}"
                                       SelectedForeground="{ThemeResource ListViewItemSelectedForegroundThemeBrush}"
                                       SelectedPointerOverBackground="{ThemeResource ListViewItemSelectedPointerOverBackgroundThemeBrush}"
                                       SelectedPointerOverBorderBrush="{ThemeResource ListViewItemSelectedPointerOverBorderThemeBrush}"
                                       SelectionCheckMarkVisualEnabled="False" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)