0

I got this xaml of a ListView:

<ListView Grid.Row="4" Grid.Column="1" 
              ItemsSource="{Binding Path=ListViewAgentItems}" 
              HorizontalAlignment="Left" 
              HorizontalContentAlignment="Left">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.View>
            <GridView>
                <GridViewColumn HeaderContainerStyle="{StaticResource ListViewStyle}" Header="Agent ID" DisplayMemberBinding="{Binding AgentId}"/>
                <GridViewColumn HeaderContainerStyle="{StaticResource ListViewStyle}" Header="Distance" DisplayMemberBinding="{Binding Distance}"/>
            </GridView>
        </ListView.View>
</ListView>

This is the style:

<Window.Resources>
    <Style x:Key="ListViewStyle" TargetType="{x:Type GridViewColumnHeader}">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
</Window.Resources>

I want both GridViewColumn to take all the space evenly, meaning that the ListView is in a grid column, so I want the GridView to take the entire column space, and also that both columns of the GridView will take 50% of the width. As of now, the ListView is automatically sized to fit the data it holds which I don't want.

CodeMonkey
  • 11,196
  • 30
  • 112
  • 203

2 Answers2

1

The simple solution is to use a datagrid rather than listview. You can set datagrid xxxx column width to *.

If you really wanted to stick with listview and you don't have a set width so you can set the widths to a specific number, see. How can I get a ListView GridViewColumn to fill the remaining space in my grid?

Andy
  • 11,864
  • 2
  • 17
  • 20
1

If you switch to a read-only DataGrid, you could use star-sizing:

<DataGrid Grid.Row="4" Grid.Column="1" 
          ItemsSource="{Binding Path=ListViewAgentItems}" 
          IsReadOnly="True" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Agent ID" Binding="{Binding AgentId}" Width="1*"/>
        <DataGridTextColumn Header="Distance" Binding="{Binding Distance}" Width="1*"/>
    </DataGrid.Columns>
</DataGrid>
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Something strange happens with this solution. The design shows it as I want it to. However when I run the app, it adds more columns automatically with the other field members of the type inside the ListViewAgentItems – CodeMonkey May 23 '19 at 12:09
  • @YonatanNir: Oh, you should add `AutoGenerateColumns="False"`. See my edit. – mm8 May 23 '19 at 12:10
  • Why do you need the IsReadOnly? It also works without it – CodeMonkey May 23 '19 at 12:13
  • @YonatanNir: You don't need it if your properties are read-only. Otherwise, you will be able to edit them by double-clicking on the cells and then it no longer behaves as a `ListView`. That's why I added it to the sample markup. – mm8 May 23 '19 at 12:14