Solved

WPF TextBlock TextAlignment="Right" not ALIGNing when data is integer/numeric

Posted on 2011-03-23
2
1,717 Views
Last Modified: 2013-12-17
I can't get my currency column to correctly align right. If I set TextAlignment="Right" on the first two columns it works, the only thing I can glean out of this is that it has something to do with the last column being numeric (currency). I've tried using DockPanel and StackPan

I've included the XAML.

Thanks in advance for your help
<Window x:Class="Arbitrage.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" MinWidth="640" Width="650" Height="400" MinHeight="400" xmlns:my="clr-namespace:Arbitrage" Loaded="Window_Loaded" WindowStyle="SingleBorderWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" >
    
    <Window.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="White" Offset="0" />
            <GradientStop Color="#FF151580" Offset="0.872" />
        </LinearGradientBrush>
    </Window.Background>
    
    <Window.Resources>
        <DataTemplate x:Key="expenseTemplate">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="80"></ColumnDefinition>
                    <ColumnDefinition Width="170"></ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <TextBlock TextAlignment="Left" Text="{Binding PostedDate, StringFormat={}{0:MM/dd/yyyy}}" Grid.Column="0" />
                <TextBlock TextAlignment="Left" Text="{Binding Description}" Grid.Column="1" />
                <TextBlock TextAlignment="Right" Text="{Binding PostAmount, StringFormat={}{0:C}}" Grid.Column="2" />                    
            </Grid>                                            
        </DataTemplate>
    </Window.Resources>

    <Grid>
        <WrapPanel HorizontalAlignment="Stretch" Margin="0,0,0,0" Name="wrapPanel1" VerticalAlignment="Top" Orientation="Horizontal" >
            <Label Content="Programs" MinWidth="200" Margin="10,10,200,0" />
            <Label Content="Revenues" MinWidth="200" Margin="10,10,0,0" />
            <ComboBox Name="cboPrograms" MinWidth="200" Margin="10,0,200,0" DisplayMemberPath="Description" ItemsSource="{Binding}" SelectedValuePath="ProgramID" SelectionChanged="cboPrograms_SelectionChanged" />
            <ComboBox Name="cboRevenues" MinWidth="200" Margin="10,0,0,0" DisplayMemberPath="Description" ItemsSource="{Binding}" SelectedValuePath="RevenueID" />
            <Label Content="Expenses" MinWidth="550" Margin="10,10,50,0" />
            <ListBox Name="lstExpenses" SelectionMode="Extended" MinWidth="350" MinHeight="200" MaxHeight="200" Margin="10,0,0,0" ItemsSource="{Binding}" ItemTemplate="{StaticResource expenseTemplate}" />               
        </WrapPanel>
    </Grid>
</Window>

Open in new window

0
Comment
Question by:revstudio
2 Comments
 
LVL 27

Accepted Solution

by:
MikeToole earned 500 total points
ID: 35205724
The problem lies in the '*' sizing of the last column.
WPF seems to use the text width as the column size on a row-by-row basis. The data is right-aligned, but in the space of its own width. (Add a background color to observe the effect.)
It can be fixed by specifying a Width in the DataTemplate for either the 3rd column, or the Grid, e.g:
            <Grid Width="350">
0
 

Author Comment

by:revstudio
ID: 35206492
Ya know that thought actually crossed my mind at some point yesterday and I dismissed it.

Tried specifying a width for the column this morning and it worked perfectly.

Sometimes we can't see the forest for the trees.

Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
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…

929 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

9 Experts available now in Live!

Get 1:1 Help Now