3

I'm trying to achieve a ToggleButton control template for listbox items. This is to be used in an application where the user can click on the listbox items to show a certain piece of functionality.

The listbox item template is defined as follows:

    <Style x:Key="ExampleListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <ToggleButton IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"
                                  HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch">
                        <StackPanel Orientation="Vertical">
                            <TextBlock x:Name="ExampleTitle" Grid.Row="0" Foreground="#333333" 
                                       FontFamily="pack://application:,,,/Resources/Fonts/#Neuropol Regular" 
                                       FontSize="16" Height="26" TextAlignment="Left" HorizontalAlignment="Left" 
                                       VerticalAlignment="Top" Text="{Binding ExampleDisplayName}"
                                       Margin="5"></TextBlock>
                            <TextBlock Grid.Row="1" Foreground="#333333" Margin="5,-5,5,3" HorizontalAlignment="Stretch" 
                                       TextAlignment="Left" FontFamily="Verdana" VerticalAlignment="Top" 
                                       TextWrapping="Wrap" Text="{Binding ExampleDescription}"/>
                        </StackPanel>
                    </ToggleButton>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

and the listbox is defined as

        <ListBox x:Name="_examplesListBox" 
                 SelectionMode="Single"
                 BorderBrush="Transparent"
                 Background="Transparent"
                 ItemsSource="{Binding AllExamples}" 
                 ItemContainerStyle="{StaticResource ExampleListBoxItemStyle}"
                 SelectedItem="{Binding SelectedExample, Mode=TwoWay}"/>

Here I have two textblocks, one bound to ExampleDisplayName, the other bound to ExampleDescription. The effect I am trying to achieve is to get the second textblock (description) to wrap around, constrained by the available space.

This is what I'm getting now:

SciChart Beta Examples Listbox Styling

What I'd like is for the second line showing example description to wrap based on the size of the listbox. When the application starts the listbox should auto-size to the first line + margin.

Any suggestions?

Dr. Andrew Burnett-Thompson
  • 20,980
  • 8
  • 88
  • 178

1 Answers1

7

Removing that horizontal scrollbar should help with text wrapping:

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">

I'm not quite sure how to auto-size ListBox on startup based on first text line size using only XAML.

max
  • 33,369
  • 7
  • 73
  • 84
  • Boom! One line fix. TextBlocks with text wrapping basically stretch to fit the parent. The first line is requesting a certain size so just turning off the listbox horizontal scrollbar forced it to size and wrap correctly. Thanks! – Dr. Andrew Burnett-Thompson Feb 06 '12 at 07:13