2

The code below is the complete representation of the problem. Essentially, when I have a ListBox inside a ScrollViewer the ListBox won't show its own scrollbar. Rather, it relies on the scrollbar of the parent ScrollViewer. In my situation I have my entire control wrapped in a ScrollViewer so that I get scrollbars when the MinHeight/MinWidth are violated. The ListBox is just one of numerous controls in my UserControl and I don't want my UserControl jumping to wide proportions to match the items in the ListBox. What would you add to the code below to force the ListBox to use its own scrollbar?

using System;
using System.Windows;
using System.Windows.Controls;

namespace TestExpanderWidth
{
    class Program
    {
        [STAThread]
        static void Main()
        {
            var listbox = new ListBox { Margin = new Thickness(10.0), MinWidth = 400 };
            listbox.Items.Add(new string('c', 3000));

            var sv = new ScrollViewer {HorizontalScrollBarVisibility = ScrollBarVisibility.Auto, VerticalScrollBarVisibility = ScrollBarVisibility.Auto};
            sv.Content = listbox; // remove for test

            var window = new Window { Width = 600, Height = 400 };
            window.Content = sv; // remove for test
            //window.Content = listbox; // add for test

            var application = new Application();
            application.Run(window);
        }
    }
}
6
  • If you set them to Visible (from Auto) do they show?
    – paparazzo
    Commented Aug 7, 2012 at 19:34
  • Huh? I want the scrollbar in the ListBox to show instead of the ScrollViewer's scrollbar. I'm not setting any feature of the ListBox's scrollbars directly.
    – Brannon
    Commented Aug 7, 2012 at 19:59
  • Yes you are setting scrollbar visibility VerticalScrollBarVisibility = ScrollBarVisibility.Auto
    – paparazzo
    Commented Aug 7, 2012 at 20:35
  • I am setting VerticalScrollBarVisibility on the control containing the ListBox. However, I don't want to see that scrollbar unless the listbox's MinSize is violated so clearly I don't want to set that scrollbar to be always visible.
    – Brannon
    Commented Aug 8, 2012 at 15:37
  • It is called diagnoses. Clearly you don't buy into the process.
    – paparazzo
    Commented Aug 8, 2012 at 15:43

3 Answers 3

5

The layout dialogue

ScrollViewer to ListBox: You can have as much space as you want, how much would you like?
ListBox to ScrollViewer: As much as i need to display all my items, to be precise <X, Y>.
ScrollViewer to ListBox: Sure, take this <X, Y>.
ListBox to ScrollViewer: K thx, i will expand to <X, Y> (now i do not need any scroll bars).

You will only get the ListBox to use its ScrollViewer if you restrict its size, which just does not happen if you place it in a ScrollViewer and have the alignments set to Stretch.

0
2
+50

I'm not sure if this is the best way to do this, but for all intense and purposes it seems to provide the functionality that you requested.

public MainWindow()
    {

        InitializeComponent();

    }

    public void WindowLoaded(object sender, EventArgs e) 
    {
        listbox = new ListBox { Margin = new Thickness(10.0), MinWidth = 400, MinHeight = 400 };
        listbox.Items.Add(new string('c', 3000));

        var sv = new ScrollViewer { HorizontalScrollBarVisibility = ScrollBarVisibility.Auto, VerticalScrollBarVisibility = ScrollBarVisibility.Auto };
        sv.Content = listbox; // remove for test

        this.Width = 600;
        this.Height = 400;
        this.Content = sv;

    }

    protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
    {

        base.OnRenderSizeChanged(sizeInfo);
        if (listbox != null)
            listbox.MaxWidth = this.RenderSize.Width - (int.Parse(listbox.Margin.Right.ToString()) * 4);

    }

Basically, I've assigned the ListBox a MaxWidth which is based on the Window's Render Width, and to trigger the event I've overridden the OnRenderSizeChanged event handler.

Hope this helps you and works how you want!

1
  • This gave me the best idea for how to deal with this issue. Thanks.
    – Brannon
    Commented Aug 20, 2012 at 16:34
0

I'm just learning WPF myself, but my understanding so far is that a ListBox has no ScrollBars. If you want to give that control ScrollBars you need to wrap it in a ScrollViewer. So in your case - if I understand you correctly - you need to add a second ScrollViewer that wraps only the ListBox.

1
  • 2
    I assure you that the ListBox hosts its own ScrollViewer. Without it the above code would not work in the case where the window.Content = listbox.
    – Brannon
    Commented Aug 7, 2012 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.