Solved

WPF DataTemplate Issue

Posted on 2012-12-27
8
318 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 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
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 

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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

717 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