ListBox知道它在一些遥远的ScrollViewer中吗?

Bra*_*non 2 c# wpf layout listbox scrollviewer

下面的代码是问题的完整表示.基本上,当我在ScrollViewer中有一个ListBox时,ListBox将不会显示自己的滚动条.相反,它依赖于父ScrollViewer的滚动条.在我的情况下,我将整个控件包装在ScrollViewer中,以便在违反MinHeight/MinWidth时获取滚动条.ListBox只是我的UserControl中的众多控件之一,我不希望我的UserControl跳转到大比例以匹配ListBox中的项目.你会在下面的代码中添加什么来强制ListBox使用自己的滚动条?

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

H.B*_*.B. 5

布局对话

ScrollViewerto ListBox:你可以拥有你想要的空间,你想要多少?
ListBoxto ScrollViewer:我需要尽可能多地展示我的所有物品<X, Y>.
ScrollViewerListBox:当然,拿这个<X, Y>.
ListBoxto ScrollViewer:K thx,我将扩展到<X, Y>(现在我不需要任何滚动条).

如果你限制它的大小,你将只能ListBox使用它,如果你把它放在一个并且对齐设置为,那就不会发生.ScrollViewerScrollViewerStretch