Results 1 to 2 of 2

Thread: [WPF] ListBox with ItemTemplate - keeping items same size and trimming content

Threaded View

  1. #2
    Hyperactive Member Arrow_Raider's Avatar
    Join Date
    Dec 2001
    Location
    AVR Lovers Club
    Posts
    423

    Re: [WPF] ListBox with ItemTemplate - keeping items same size and trimming content

    HorizontalContentAlignment on the ListBox must be set to "Stretch." By default it is "Left." This will cause your content to stretch to the parent control size. Next, you have to disable horizontal scrolling. Otherwise, the content can grow indefinitely in the horizontal direction. Also, you can't use a horizontal StackPanel if you want to limit the size of an element in the horizontal direction. I replaced that StackPanel with a Grid. Finally, to enable text trimming with the ellipsis effect, the TextTrimming property is set to CharacterEllipsis on the message TextBlock.

    Here is the final resultant xaml:
    Code:
    <ListBox Name="lstErrors"
                     Grid.Row="1"
                     Grid.Column="0"
                     Margin="6"
                     HorizontalContentAlignment="Stretch"
                     ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Border Margin="4"
                                BorderThickness="1"
                                BorderBrush="SteelBlue"
                                CornerRadius="3">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Image Name="imgError"
                                       Margin="6"
                                       Source="/WpfLogfileReader;component/Images/109_AllAnnotations_Error_256x256.png"
                                       Width="24"
                                       Height="24" />
                                <StackPanel Grid.Column="1">
                                    <TextBlock FontWeight="Bold"
                                               Text="{Binding Path=Date}" />
                                    <TextBlock Text="{Binding Path=Message}"
                                               TextTrimming="CharacterEllipsis" />
                                </StackPanel>
                            </Grid>
                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
    Last edited by Arrow_Raider; Jan 29th, 2011 at 11:07 PM. Reason: Updated highlighting
    My monkey wearing the fedora points and laughs at you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width