Solved

WPF DataTemplate Issue

Posted on 2012-12-27
8
315 Views
Last Modified: 2013-02-12
I have a WPF application that uses data templates to bind and format a ListView control. The data templates are also supposed to control the text alignment in the cells of the ListView. Here is an example of one template:

<DataTemplate x:Key="CellID">
     <TextBlock HorizontalAlignment="Center" 
               Text="{Binding Path=ID}"/>
</DataTemplate>

Open in new window


The problem I am having is that the "center" is not applied, the text is left justified in the textblock, however the binding works correctly and the correct data is displayed.

This template is used like this:

<ListView x:Name="lstCues" Background="{x:Null}" Foreground="#FFD3F506" ItemsSource="{Binding Mode=OneWay}">
	<ListView.View>
		<GridView>
			<GridViewColumn Width="50" Header="Cue ID" CellTemplate="{StaticResource CellID}" />
			<GridViewColumn Width="200" Header="Name" CellTemplate="{StaticResource CellName}"/>
			<GridViewColumn Width="400" Header="Filename" CellTemplate="{StaticResource CellFilename}"/>
		</GridView>
	</ListView.View>
</ListView>

Anyone have ideas why this is not working? I am pretty sure this is how I have done this in the past.

Thanks,
Sid.

Open in new window

0
Comment
Question by:Sid Price
  • 4
  • 3
8 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 250 total points
ID: 38725445
Try to wrap the textblock into another element like a Border or any other container:

<DataTemplate x:Key="CellID">
    <Border HorizontalAlignment="Stretch">
        <TextBlock HorizontalAlignment="Center" Text="{Binding Path=ID}" />
    </Border>
</DataTemplate>
0
 

Author Comment

by:Sid Price
ID: 38725462
Thank you for the suggestion. I edited my code but get the same result, the text is not centered:

		<DataTemplate x:Key="CellID">
			<Border HorizontalAlignment="Stretch">
    			<TextBlock HorizontalAlignment="Center" Text="{Binding Path=ID}">
    				<TextBlock.Foreground>
    					<Binding Converter="{StaticResource IDDisplay}" />
					</TextBlock.Foreground>
				</TextBlock> 
			</Border>
		</DataTemplate>

Open in new window

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 38725493
it seems that your itempanel is not being centered, so you may try in addition, at ListView level (not at template level):

<ListView HorizontalContentAlignment="Stretch"  ......
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:Sid Price
ID: 38725501
I added that but no change:

<ListView x:Name="lstCues" HorizontalAlignment="Stretch" Background="{x:Null}" ItemsSource="{Binding Mode=OneWay}" BorderBrush="{x:Null}" Grid.Row="1">

Open in new window

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 38725505
It is HorizontalContentAlignment and not HorizontalAlignment
0
 

Author Comment

by:Sid Price
ID: 38725582
Sorry. my mistake. Here is the edited code, however it still doesn't work:

<ListView x:Name="lstCues" HorizontalContentAlignment="Stretch" Background="{x:Null}" ItemsSource="{Binding Mode=OneWay}" BorderBrush="{x:Null}" Grid.Row="1">

Open in new window

0
 

Accepted Solution

by:
ramgev08 earned 250 total points
ID: 38829004
you need to override the ListViewItem style in order to stretch its content.

add this to your resources should do the trick

                    <Style TargetType="{x:Type ListViewItem}">
                        <Setter Property="HorizontalContentAlignment"
                                Value="Stretch" />
                    </Style>
0
 

Author Comment

by:Sid Price
ID: 38882743
Thank you ramgev08, that was the final step that got my list view to show a centered column.
Sid.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Javascript and Jquery not firing 9 48
Write to file when application closes (by logoff) - VB.NET 1 31
SQL Help 27 42
C# LINQ ForEach() question 6 19
IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question