Solved

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

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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