WPF - Listview项目保证金

时间:2013-04-01 16:35:42

标签: c# wpf xaml

我的Listview项目未扩展到列的整个宽度。我的细胞边界右侧总是有一个边距。我想让边界区域延伸到整个列宽并摆脱任何填充,边距或我内容左右两侧的任何其他内容。目标是让边框填满每个单元格的整个空间。

我已经应用了拉伸并将边距设置为“-6,0,-6,0”,但这似乎无法解决问题。

这是我的代码:

  <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 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

只需为Border元素设置边距:

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

答案 2 :(得分:1)

替代解决方案

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

答案 3 :(得分:0)

我是用

做的
HorizontalContentAlignment="Stretch"
ListView上的