0

I have problem with HorizontalAlignment of TextBlock in ListBox ItemTemplate. This is part of my code:

<ListBox x:Name="mealList" ItemsSource="{Binding Meals}" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="auto"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Name}" Margin="10" Grid.Column="0"  TextWrapping="Wrap" Style="{StaticResource PhoneTextSmallStyle}"/>
                <TextBlock Text="{Binding Price}"  Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center" Style="{StaticResource PhoneTextSmallStyle}"/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

In App I have this list of things:

Actual state

But I want, that right column with "xx,- Kč" has HorizontalAlignment="Right". Every item in this column should be on the righ side of screen and below previous item. Second column in second row should be below second column in first row. Do you understand?

Can you help me? I can post more of code.

Pepa Zapletal
  • 2,879
  • 3
  • 39
  • 69
  • I think the problem is within the column definitions. The `*` value uses the available space, but the problem are the line breaks in your text. The width of the first column is the maximum length of a text line. If you draw the borders of the textblocks it may be clear. – xmashallax Jul 10 '13 at 22:33
  • The problem is that you have a new grid for each listItem. Try using percentage values for the column widths, like "8*" and "2*". – Jon Jul 11 '13 at 00:23
  • I try use percentage values, but I didn´t help. Have someone another tip?? – Pepa Zapletal Jul 11 '13 at 08:10
  • 1
    Look at this post: http://stackoverflow.com/questions/838828/how-to-get-a-listbox-itemtemplate-to-stretch-horizontally-the-full-width-of-the – Jon Jul 11 '13 at 18:17
  • to late reply, but this worked for me: http://stackoverflow.com/questions/6782452/make-grid-align-properly – PAG Nov 08 '13 at 03:48
  • It's a common mistake, @Pyush has right. You have to add the style `` above – general03 Sep 02 '14 at 08:34

0 Answers0