5

My Listview items are not stretching to the full width of the column. 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.

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>
4

4 に答える 4

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>
于 2013-04-01T17:10:25.263 に答える
1

代替ソリューション

typeof(GridViewRowPresenter).GetField("_defalutCellMargin", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.GetField).SetValue(null, new Thickness(0));
于 2020-01-09T12:42:02.327 に答える
0

私はそれを行います

HorizontalContentAlignment="Stretch"

リストビューで

于 2016-08-08T11:42:09.813 に答える