c# - WPF - ListView 项目边距

标签 c# wpf xaml

我的 Listview 项目没有拉伸(stretch)到列的整个宽度。我的单元格边框右侧始终有一个边距。我想让边框区域延伸到整个列宽,并去掉内容左右两侧的任何填充、边距或任何其他内容。目标是让边框填满每个单元格中的整个空间。

我已经应用了拉伸(stretch)并将边距设置为“-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>

最佳答案

我使用 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>

关于c# - WPF - ListView 项目边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15747365/

相关文章:

c# - 在 C# 中从没有颜色对象的像素中提取 RGB

c# - .net 中的 Powershell 版本兼容性

c# - 如何在 db.SaveChanges() 之前更改 Controller 中的 http post 值?

WPF ListView VerticalScrollBar 未显示

c# - 获取 WPF UIElement 的左上角坐标

c# - 执行更改 UI 的方法时,是否必须在 DIspatcher 上调用 CheckAccess()?

c# - 导航期间内存增加,但堆看起来不错?

c# - 自动选择焦点上的所有文本 Xamarin

c# - WPF中如何同步ListBox SelectedItem和焦点项?

c# - DragEnter 和 DragLeave : Avoid nasty flickering on custom drag preview