Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

WPF DataTemplate Issue

Posted on 2012-12-27
8
Medium Priority
?
320 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

604 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