Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-23
2
Medium Priority
?
1,976 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
[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
2 Comments
 
LVL 27

Accepted Solution

by:
MikeToole earned 2000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

715 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