WPF - overview item list mark

My Listview objects do not stretch to the full width of the column. Thereโ€™s always a margin to the right of the border of my cell. I would like the boundary region to stretch over the entire width of the column and get rid of any additions, margins, or anything else to the left and right of my content. The goal is for borders to fill all the space in each cell.

I already applied the stretch and set the margins to "-6.0, -6.0", but this does not 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
source share
3 answers

I got it working using the DataTemplate resource and set the field border to -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> 
+6
source

Just set the field value for the Border element:

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

I do it with

 HorizontalContentAlignment="Stretch" 

in listview

0
source

All Articles