Solved

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

Posted on 2011-03-23
2
1,904 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 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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

626 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