views:

259

answers:

2

Hi, Within a Groupbox I have a Listbox, ListboxItems are defined in the XAML as well. The Listbox is defined:

<ListBox Name="lvAvoidCountry" Margin="5,5,5,5"  
    Background="Transparent" 
    ScrollViewer.VerticalScrollBarVisibility="Visible"
    ScrollViewer.HorizontalScrollBarVisibility="Disabled" >

Items are defined like this:

<ListViewItem >
  <CheckBox Name="chkAlbanien" Tag="55">
    <StackPanel Orientation="Horizontal">
      <Image Source="images/flag_albania.png" Height="30"></Image>
      <TextBlock Text="Albanien" Margin="5,0,0,0"></TextBlock>
    </StackPanel>
  </CheckBox>
</ListViewItem>

If I remove the Scrollviewer Settings I get horizontal scrolling and the Items are well formatted - correct width. If I use the scrollviewer settings the items get cut off so that all items are placed on the listbox. (eg. the flag is shown, the checkbox is shown but the text is just "Alba").

Thanks for any hints!

A: 

As the name implies, ScrollViewer.HorizontalScrollBarVisibility="Disabled" disables horizontal scrolling. If you do that, but your ListBoxItems are too long, they'll get cut off. The StackPanel won't grow or shrink to fit into the ListBox, and it won't "wrap" your items to fit into the ListBox if it's too narrow, even if you add TextWrapping to the TextBlock. It's very stubborn. I think your main problem is that StackPanel.

Instead of a StackPanel, try using a Grid with 2 columns defined like so:

<ListViewItem >
  <CheckBox Name="chkAlbanien" Tag="55">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Image Grid.Column="0" Source="images/flag_albania.png" Height="30"/>
        <TextBlock Grid.Column="1"
                   TextWrapping="Wrap"
                   Text="Albanien" Margin="5,0,0,0"/>
    </Grid>
  </CheckBox>
</ListViewItem>

Auto will "shrinkwrap" the image columns, and * will give the text all remaining space. Then add TextWrapping to your textblock in case it's still too long.

Edited: added more complete code example and changed my answer slightly.

Benny Jobigan
Hi, thanks for the answer - I want the listbox to look like|================================|| x 1 x 2 x 3 x 4 x 5 || x 6 x 7 x 8 x 9 x 10 |but currently it "draws" "x 6 and x 7" horizontal to the right - out of sight and starts the next row with "x 8".I want it to only use the visible width to "draw" teh items and allow it scroll vertically,...I guess with the ColumDefinitions I will only get one Column with items,...
JerryVienna
So you don't want a single listboxitem per row, stacked vertically...you want listboxitems arranged horizontally so they "flow" like text? (Sorry, I'm having difficulty visualizing what you want. If you could draw a diagram and add it to your question, it might help.)
Benny Jobigan
A: 

if you want vertical scrolling in a listbox then don't put it in a stackpanel,instead use a grid.

Doktor83