locked
TextWrapping in ListboxItem as Stackpanel RRS feed

  • Question

  • Hi,

    i now tried some hours for realizing a textwrapping of a textblock that is included in a stackpanel as a listbox item. Besides of the the textblock there is a image included in the stackpanel. my goal ist to have the image on the left and followed by the describing text. unfortunately the textwrapping just works when the orientation of the stackpanel is set to vertical. As you can see i tried several positions of the textwrapping but nothing works. thanks for your help.

    Here is my code:

    1                            <ListBox.ItemContainerStyle>
    2                                <Style TargetType="ListBoxItem">
    3                                    <Setter Property="Width" Value="215" />
    4                                    <Setter Property="TextWrapping" Value="Wrap" />
    5                                    <Setter Property="Template">
    6                                        <Setter.Value>
    7                                            <ControlTemplate>
    8                                                <Border BorderThickness="1" BorderBrush="#FF600000"
    9                                                        Background="White">
    10                                                   <ContentPresenter Content="{TemplateBinding Content}"
    11                                                       ContentTemplate="{TemplateBinding ContentTemplate}"
    12                                                       TextWrapping="Wrap" />
    13                                               </Border>
    14                                           </ControlTemplate>
    15                                       </Setter.Value>
    16                                   </Setter>
    17                               </Style>
    18                           </ListBox.ItemContainerStyle>
    19                           <ListBox.ItemTemplate>
    20                               <DataTemplate>
    21                                   <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
    22                                       <Image Source="{Binding itemImage}"/>
    23                                       <TextBlock Text="{Binding itemText}" TextWrapping="Wrap"/>
    24                                   </StackPanel>
    25                               </DataTemplate>
    26                           </ListBox.ItemTemplate>
    

    Here is like it looks:




    Thursday, August 14, 2008 1:43 PM

Answers

  • set the width on the TextBlock in Datatemplate

    Thursday, August 14, 2008 4:42 PM

All replies

  • set the width on the TextBlock in Datatemplate

    Thursday, August 14, 2008 4:42 PM
  • Thanks.. sometimes one doesn't see the simpelst solutions...
    Friday, August 15, 2008 7:53 AM
  • Lee,

    What about resizing?  Then setting a specified width would not work. 

    L4G

    Saturday, July 24, 2010 3:46 PM
  • A StackPanel will not work in this scenario. Use a Grid or a DockPanel. 

    Saturday, July 24, 2010 6:04 PM