简体   繁体   English

WPF - 列表视图项边距

[英]WPF - Listview Item Margin

My Listview items are not stretching to the full width of the column.我的 Listview 项目没有拉伸到列的整个宽度。 There is always a margin on the right of my cell border.我的单元格边框右侧总是有一个边距。 I would like to get the bordered area to stretch over the full column width and get rid of any padding, margins or anything else on the left and right of my content.我想让带边框的区域在整个列宽上伸展,并去除内容左右两侧的任何填充、边距或任何其他内容。 The goal is to have the borders fill the whole space in each cell.目标是让边框填充每个单元格中的整个空间。

I have already applied stretching and set the margins to "-6,0,-6,0" but that doesn't seem to solve the problem.我已经应用了拉伸并将边距设置为“-6,0,-6,0”,但这似乎并没有解决问题。

Here is my code:这是我的代码:

  <Grid>
            <Grid.Resources>
                <x:Array Type="{x:Type sys:String}" x:Key="items">
                    <sys:String>Foo</sys:String>
                    <sys:String>Bar</sys:String>
                    <sys:String>Spong</sys:String>
                </x:Array>
            </Grid.Resources>

            <ListView ItemsSource="{StaticResource items}">

                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="Margin"
            Value="-6, 0,-6,0" />
                        <Setter Property="HorizontalAlignment" Value="Stretch" />
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                        <Setter Property="VerticalContentAlignment" Value="Stretch" />
                        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
                    </Style>
                </ListView.ItemContainerStyle>

                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Data" Width="80">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <Border BorderThickness="2" BorderBrush="Black" HorizontalAlignment="Stretch">
                                        <TextBox Text="{Binding .}"  />
                                    </Border>

                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn Header="Length"
         DisplayMemberBinding="{Binding Length}" />
                    </GridView>
                </ListView.View>
            </ListView>

        </Grid>

I got it working using a DataTemplate resource and setting the border's margin to -6.我使用 DataTemplate 资源使其工作并将边框的边距设置为 -6。

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <x:Array Type="{x:Type sys:String}" x:Key="items">
                <sys:String>Foo</sys:String>
                <sys:String>Bar</sys:String>
                <sys:String>Spong</sys:String>
            </x:Array>
            <DataTemplate x:Key="MyDataTemplate">
                <Border BorderThickness="2" BorderBrush="Black" Margin="-6">
                    <TextBox Text="{Binding .}" Margin="0" Padding="0"/>
                </Border>
            </DataTemplate>
        </Grid.Resources>
        <ListView ItemsSource="{StaticResource items}">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.View>
                <GridView >
                    <GridViewColumn Header="Data" Width="80" CellTemplate="{StaticResource MyDataTemplate}" />
                    <GridViewColumn Header="Length" DisplayMemberBinding="{Binding Length}" />
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

Just set the Margin for your Border element:只需为您的边框元素设置边距:

            <Border Margin="-6,0,-6,0" BorderThickness="2" BorderBrush="Black" HorizontalAlignment="Stretch">
               <TextBox Text="{Binding .}"  />
            </Border>

替代方案

typeof(GridViewRowPresenter).GetField("_defalutCellMargin", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.GetField).SetValue(null, new Thickness(0));

I do it with我这样做

HorizontalContentAlignment="Stretch"

on ListView在列表视图上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM