0
votes

I am new in WPF. In WPF application I used Listview

showing snap of listview below:-

enter image description here

Problem is that the empty space between grid column header and vertical scroll bar in listview (gridview)

I think its because of last blank column of grid is there any way to remove blank column or any alignment property for this to extend the width of my column "Device Name"

Below is my XAML for Listview-Gridview

 <ListView x:Name="lstviewDevices"  Height="263"  ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalAlignment="Left" Margin="37,89,0,0" VerticalAlignment="Top" Width="128" SelectionChanged="lstviewDevices_SelectionChanged">
 <ListView.Resources>

             <Style TargetType="{x:Type GridViewColumnHeader}">
                    <Setter Property="HorizontalContentAlignment" Value="Left" />
                    <Setter Property="Background" Value="#adb8ca" />
                    <Setter Property="Foreground" Value="#fff" />
                    <Setter Property="FontSize" Value="15" />
                    <Setter Property="FontFamily" Value="sans-serif" />
                    <Setter Property="FontWeight" Value="SemiBold" />

                    <!--<Setter Property="Margin" Value="3,0,-7,1" />-->
                </Style>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="HorizontalContentAlignment" Value="Left" />
                    <Setter Property="Background" Value="White" />
                    <Setter Property="Foreground" Value="#337ab7" />
                    <Setter Property="FontSize" Value="13" />
                    <Setter Property="FontFamily" Value="Roboto" />

                </Style>
            </ListView.Resources>
            <ListView.View>
                    <GridView>
                    <GridViewColumn   Header="Device Name" Width="Auto"  DisplayMemberBinding="{Binding DeviceName}"/>
                </GridView>
            </ListView.View>


        </ListView>

Are there any inputs on same ?

thanks in advance

2
try with Width="*" on GridViewColumnASh
It shows compile time error:- "Can not convert '*"Vsagar

2 Answers

1
votes

I have modified your code a little.. try setting padding in your listview and modify it according to your requirement

   <ListView x:Name="lstviewDevices" Padding="0,0,-3,0"  Height="263"  ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalAlignment="Left" Margin="37,89,0,0" VerticalAlignment="Top" Width="228" >
            <ListView.Resources>

                <Style TargetType="{x:Type GridViewColumnHeader}">
                    <Setter Property="HorizontalContentAlignment" Value="Left" />
                    <Setter Property="Background" Value="#adb8ca" />
                    <Setter Property="Foreground" Value="#fff" />
                    <Setter Property="FontSize" Value="15" />
                    <Setter Property="FontFamily" Value="sans-serif" />
                    <Setter Property="FontWeight" Value="SemiBold" />

                    <!--<Setter Property="Margin" Value="3,0,-7,1" />-->
                </Style>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="HorizontalContentAlignment" Value="Left" />
                    <Setter Property="Background" Value="White" />
                    <Setter Property="Foreground" Value="#337ab7" />
                    <Setter Property="FontSize" Value="13" />
                    <Setter Property="FontFamily" Value="Roboto" />

                </Style>
            </ListView.Resources>
            <ListView.View>
                <GridView>
                    <GridViewColumn   Header="Device Name" Width="Auto"  DisplayMemberBinding="{Binding DeviceName}"/>
                </GridView>
            </ListView.View>


        </ListView>
0
votes

you can use a grid to define column width and then bind the gridview with grid.

<Grid Grid.Row="1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <Grid Grid.Column="0" x:Name="col1"/>
                    <Grid Grid.Column="1" x:Name="col2"/>
                </Grid>
                <ListView Grid.Row="1">
                    <ListView.View>
                        <GridView>
                            <GridView.Columns>
                                <GridViewColumn Header="col1Header" Width="{Binding ElementName=col1, Path=ActualWidth}">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding}"/>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                                <GridViewColumn Header="col2Header" Width="{Binding ElementName=col2, Path=ActualWidth}">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding}"/>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                            </GridView.Columns>
                        </GridView>
                    </ListView.View>
                </ListView>