?
Solved

WPF DataTemplate Issue

Posted on 2012-12-27
8
Medium Priority
?
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 750 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

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 750 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

762 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