Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a ListView with three columns. The first two have widths that don't change much (always contain the same number of characters). I can fix the widths of these columns if necessary.

The third column contains notes that can be any length. I would like this column to stretch to fill available space, to display as much of the note as possible. I can do this by giving the column a width of Auto, but the header text is always centred and so disappears off the right-hand edge.

The problem is made worse because this ListView is the centre (of three) panels made using a Grid, and I would like this central panel to be the one that grows and shrinks as the window size is changed.

How can I do this?

What I have tried:

I have tried following various examples but none seems to work.

This is my current code, which doesn't work, but I don't understand why:

<ListView Grid.Row="0" Grid.Column="1" SelectionMode="Single" ItemsSource="{Binding EnquiryJobsFiltered}">
	<ListView.View>
		<GridView>
			<GridViewColumn Header="Enquiry/Job" Width="90" DisplayMemberBinding="{Binding EnquiryJobNumberAsString}"/>
			<GridViewColumn Header="Progress" Width="90" DisplayMemberBinding="{Binding Progress}"/>
			<GridViewColumn>
				<GridViewColumn.HeaderTemplate>
					<DataTemplate>
						<TextBlock Text="Description" TextAlignment="Left"/>
					</DataTemplate>
				</GridViewColumn.HeaderTemplate>
				<GridViewColumn.CellTemplate>
					<DataTemplate>
						<TextBlock Text="{Binding Description}" TextAlignment="Left"/>
					</DataTemplate>
				</GridViewColumn.CellTemplate>
			</GridViewColumn>
		</GridView>
	</ListView.View>
</ListView>
Posted
Updated 22-Jun-17 8:56am
v2

1 solution

It should be as simple as adding a HeaderContainerStyle and setting the HorizontalContentAlignment to Left:
<GridViewColumn Header="Description" DisplayMemberBinding="{Binding Description}">
    <GridViewColumn.HeaderContainerStyle>
        <Style TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="HorizontalContentAlignment" Value="Left" />
        </Style>
    </GridViewColumn.HeaderContainerStyle>
</GridViewColumn>

How-to: ListView with left aligned column names[^]
 
Share this answer
 
Comments
Patrick Skelton 23-Jun-17 4:39am    
Thank you, Richard! Your phrase 'It should be as simple as...' brought a smile to my face. Even after a year, some aspects of XAML still seem like a black art to me, and judging by the number of wild goose chases I follow on the internet while looking for solutions, it seems I am not alone. I still seem often to take an approach of randomly setting properties, just to see if I can get anything to move on the screen. Anyway, thanks again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900