如何在 C# 中向窗口添加滚动条

发布于 2024-11-08 14:21:13 字数 126 浏览 0 评论 0原文

我创建了一个窗口,如下所示:

Window myWindow = new Window();

如何向此窗口添加垂直滚动条,并使滚动条仅在高度不够大以显示所有元素时才可见。

I have created a window as follows:

Window myWindow = new Window();

How can I add a Vertical Scroll Bar to this Windows and make the Scroll Bar only visible if the Height isn't large enough to show all the elements.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哥,最终变帅啦 2024-11-15 14:21:13

您可以将 ScrollViewer 元素添加到窗口中,并将必要的控件放入 ScrollViewer 中控制。

<ScrollViewer VerticalScrollBarVisibility="Auto">
    ...
</ScrollViewer>

或者,如果您想在代码隐藏文件中对其进行编码,您可以编写

ScrollViewer viewer = new ScrollViewer();
viewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
// append scroll viewer to window

You could add a ScrollViewer element to your window and put the necessary controls into the ScrollViewer control.

<ScrollViewer VerticalScrollBarVisibility="Auto">
    ...
</ScrollViewer>

Or if you want to code it in the code-behind file you could write

ScrollViewer viewer = new ScrollViewer();
viewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
// append scroll viewer to window
风情万种。 2024-11-15 14:21:13

您不能将滚动条添加到窗口本身。您只能向控件添加滚动条。 IE 到窗口内的网格。

示例:

<Grid  ScrollViewer.CanContentScroll="True"
       ScrollViewer.HorizontalScrollBarVisibility="Auto">
   ...
</Grid>

编辑:

刚刚意识到 Window 也有一个 ScrollViewer 属性。我不确定这个属性如何适用于窗口以及这样的窗口会是什么样子。尝试了一下,但没有出现滚动条。

编辑2:

ScrollViewer sv = new ScrollViewer();
sv.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
myGrid.Children.Add(sv);

You cannot add a scrollbar to a window itself. You can only add scrollbars to controls. I.E. to a grid inside your window.

Example:

<Grid  ScrollViewer.CanContentScroll="True"
       ScrollViewer.HorizontalScrollBarVisibility="Auto">
   ...
</Grid>

EDIT:

Just realized that Window also has a ScrollViewer property. I'm not sure how this property works for a Window and how such a window would look like. Gave it a try, but no scrollbars show up.

EDIT 2:

ScrollViewer sv = new ScrollViewer();
sv.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
myGrid.Children.Add(sv);
烟花易冷人易散 2024-11-15 14:21:13

试试这个

var xpage = your user control or page to which scroll bar need to be added at runtime

            xpage.SetValue(ScrollViewer.CanContentScrollProperty, true);
            xpage.SetValue(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Auto);
            xpage.SetValue(ScrollViewer.VerticalScrollBarVisibilityProperty, ScrollBarVisibility.Auto);

            var scrollViewer = xpage.Content as ScrollViewer;
            if (scrollViewer != null)
            {
                var content = scrollViewer.Content;
                scrollViewer.Content = null;
                xpage.Content = content;
            }
            else
            {
                var content = xpage.Content;
                xpage.Content = null;
                xpage.Content = new ScrollViewer { Content = content };
            }

try this

var xpage = your user control or page to which scroll bar need to be added at runtime

            xpage.SetValue(ScrollViewer.CanContentScrollProperty, true);
            xpage.SetValue(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Auto);
            xpage.SetValue(ScrollViewer.VerticalScrollBarVisibilityProperty, ScrollBarVisibility.Auto);

            var scrollViewer = xpage.Content as ScrollViewer;
            if (scrollViewer != null)
            {
                var content = scrollViewer.Content;
                scrollViewer.Content = null;
                xpage.Content = content;
            }
            else
            {
                var content = xpage.Content;
                xpage.Content = null;
                xpage.Content = new ScrollViewer { Content = content };
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文