hyankov
hyankov

Reputation: 4120

Resize inner control width when vertical scrollbar shows up

I have:

<DockPanel>
  <!-- Left Panel -->
  <LocalViews:MyView1 DockPanel.Dock="Left" Width="250" />

and in MyView1

<DockPanel>
 <Button ...

 <ListBox>
   <ItemsControl.ItemTemplate>
    <DataTemplate>
      <GroupBox>
        <!-- stuff here -->
        ...
    </DataTemplate>

I don't know if that's relevant, but I have completely overridden the default ListItem style, because I want it to look like an ItemTemplate, but with support of SelectedItem:

<Style TargetType="ListBox" x:Key="XXX" BasedOn="{StaticResource {x:Type ListBox}}">
    <Style.Resources>
        <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <ContentPresenter />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Style.Resources>
</Style>

Vertical scrollbar doesn't show by default (good) and when I resize the window and decrease the available height, it shows up (also good). Problem is, the vertical scrollbar causes a horizontal scrollbar. I don't want that.

enter image description here

enter image description here

What's driving me crazy is that in the Designer (VS) it looks fine:

enter image description here

I have seen solutions where an additional space is left on the side of the inner control, to allow for the scrollbar to appear and another solution where the scrollbar is visible at all times so it doesn't 'jump out'. I don't like either.

My question is - what style do I apply to the listbox item template so that when the vertical scrollbar shows up, the item will resize its width to fit the now shrinked available horizontal space?

Upvotes: 1

Views: 2362

Answers (2)

dba
dba

Reputation: 1175

Have you tried:

<DockPanel>
  <Button DockPanel.Dock="Top" />
  <ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled"
           ScrollViewer.VerticalScrollBarVisibility="Auto"
           HorizontalContentAlignment="Stretch">
    <ListBox.ItemTemplate>
      <DataTemplate>
        <GroupBox>
          ...Your Content Here...
        </GroupBox>
      </DataTemplate>
    </ListBox.ItemTemplate>
  </ListBox>
</DockPanel>

I can't see, what your ListBox-Style should do for you, especially that you don't refer to the style in your Sample snippet... However...

Your ListboxItems should fit the horizontal space now, with or without the vertical scrollbar

Upvotes: 0

Joe
Joe

Reputation: 7004

How are you sizing your window(Or parent container)? Without any more information to go on, there's only a small case where both scroll bars appear like you describe - where the width of container hosting the scroll viewer is between the minimum width of your content, and the minimum width of the content plus the width of the scroll bar itself.

Normally, the vertical scroll bar appears no problem when the height decreases below the minimum height. Similarly with the horizontal and width. It's only when these two coincide where they "trigger" each other. Here's all three cases, see how it's only a 10by10 (ish) pixel size where the triggering occurs? This is pretty unlikely:

enter image description here

The only reason I can see this being a problem is if it's the default state. The chances of the user happening to have their window size at the correct point for this to be a regular occurance is pretty minimal. If it does happen, say at 'key' resolutions and your content is fixed - change the width of the internals by a few pixels until it doesn't.

The only other time this would occur regularly is if you are sizing your window based on the content, say with SizeToContent="WidthAndHeight" (or SizeToContent="Width"). This tells your window to size itself to the minimum possible width. The problem is, this is always in the case where adding a scroll bar will cause this behavior.

You could start your window maximized, or at a sensible resolution that's fixed. Or create some sensible logic/behavior to adjust the starting window size a little bigger than SizeToContent="WidthAndHeight" provides.


Edit

I see you set the width of the parent container as an absolute 250. Try setting this to 270 (The default width of a scrollbar on windows 10 is 17px).

Are you setting the content of the ListBox manually to 250 width somewhere as well? If the contents and the container are both 250, when the scrollbar appears there's nothing else for it to do but horizontal scroll.

As this doesn't happen with an identical structure on my end:

enter image description here

The natural conclusion is it's the content of the group box. Try commenting this out to confirm. Something isn't playing well with it's layout!

Upvotes: 3

Related Questions