gpt4 book ai didi

c# - ListBox 知道它在某个遥远的 ScrollViewer 中吗?

转载 作者:行者123 更新时间:2023-11-30 20:05:10 24 4
gpt4 key购买 nike

下面的代码是问题的完整表示。本质上,当我在 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);
}
}
}

最佳答案

布局对话

ScrollViewerListBox : 您可以想要多少空间,您想要多少?
ListBoxScrollViewer : 准确地说,我需要展示我所有的元素 <X, Y> .
ScrollViewerListBox : 当然,拿这个 <X, Y> .
ListBoxScrollViewer : K thx,我会扩展到<X, Y> (现在我不需要任何滚动条)。

您只会得到 ListBox使用它的 ScrollViewer如果你限制它的大小,如果你把它放在 ScrollViewer 中就不会发生这种情况并将路线设置为 Stretch .

关于c# - ListBox 知道它在某个遥远的 ScrollViewer 中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11851491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com