Solved

WPF DataTemplate Issue

Posted on 2012-12-27
8
313 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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now