以编程方式向滚动查看器添加内容,滚动条停止工作 [英] Programmatically adding content to scrollviewer, scrollbar stops working

查看:45
本文介绍了以编程方式向滚动查看器添加内容,滚动条停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我不是那么精通强大的 WPF,但我尝试了一个有趣的项目来跳入它.我制作了一个简单的 RSS/ATOM 提要查看器,它从 RRS 或 ATOM 提要中提取 HTML,并将其粘贴到添加到堆栈面板的浏览器控件中......这是 ScrollViewer 的内容.哇.无论如何,问题是,我在后面的代码中完成了这一切,发现 ScrollViewer 不起作用,或者无法识别内容的大小,因此没有滚动.我尝试设置查看器和内容的大小,以及最小和最大大小.

Ok, so I am not that versed in the mighty WPF, but I attempted an interesting project to jump into it. I have made a simple RSS/ATOM feed viewer that pulls the HTML out of and RRS or ATOM feed and sticks it in a Browser control which is added to a stack panel... which is the content of a ScrollViewer. Whew. Anyways the problem is, I am doing this all in the code behind and have found that the ScrollViewer doesn't work, or isn't recognizing the size of the content, so there is no scrolling. I have tried setting the size of the viewer and the content, as well as attempted the min and max sizes.

我在这里错过了什么?内容就在那里,如果我在加载 WPF 之前加载它,它就可以工作,但是一旦我尝试更改或清除"控件中的子项,滚动查看器就会停止正常工作.

What am I missing here? The content is there, and if I load this before the WPF is loaded it works but once I try to change, or "Clear" children from a control, the scrollviewer stops working right.

<Window x:Class="Heine.Syndication.xkcd.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Feed Viewer" Height="600" Width="800">
<StackPanel>
    <ToolBarPanel >
        <ToolBar>
            <ComboBox Margin="0" Width="100" Name="cbController">
                <MenuItem Header="xkcd" Name="xkcdMI"/>
                <MenuItem Header="9Gag" Name="nineGagMI"/>
                <MenuItem Header="reddit" Name="redditMI"/>
            </ComboBox>
        </ToolBar>
    </ToolBarPanel>
    <Grid Name="svMain">

    </Grid>
</StackPanel>
</Window>

    public MainWindow()
    {
        InitializeComponent();

        cbController.SelectedIndex = 0;

        xkcdMI.Click += xkcdMI_Click;
        nineGagMI.Click += nineGagMI_Click;
        redditMI.Click += redditMI_Click;

        Load("http://xkcd.com/atom.xml");
    }

    private void Load(string feedUrl)
    {
        var reader = XmlReader.Create(feedUrl);
        var feed = SyndicationFeed.Load<SyndicationFeed>(reader);
        svMain.Children.Clear();

        var tmpStack = new StackPanel();


        foreach (var item in feed.Items)
        {
            var browser = new WebBrowser();

            GetHTML(ref browser, item);

            tmpStack.Children.Add(browser);
        }

        svMain.Children.Add(new ScrollViewer()
        {
            Content = tmpStack,
            Height = svMain.Height
        });
    }

推荐答案

好吧,我很遗憾地回答了我自己的问题,没有发疯并重写一堆东西.因此,在我的研究中,事实证明在 .NET 4.0 和 4.5 中,StackPanel 与 ScrollView 配合得很好……只要您知道自己在做什么!我同意留下的评论,即 MVVM 是后台发生的事情,我的代码实际上反映了我必须将其更改为尝试使其工作的内容,即使我有适当的模型、视图、侦听器/处理程序等(都内置在框架中).

Okay, so I am unfortunately answering my own question, without going crazy and rewriting a bunch of stuff. So in my research, it turns out that in .NET 4.0 and 4.5, StackPanel is great with ScrollView... so long as you know what you are doing! I agree with the comments left that MVVM is what is happening in the background, and my code actually reflects what I had to change it to to try and get it working, even when I had proper models, views, listeners/handlers etc (which are all built into the framework).

所以我的问题的答案,鉴于上述情况,以及 这个链接 我发现设置我的网格的大小,其中包含滚动视图和其他这样的乐趣使它像宣传的那样工作.问题显然是 Grid 向滚动视图报告它不可定义地大,因此滚动查看器也可能如此.所以...对于我上面的代码,我需要处理整个表单的大小并相应地设置网格的高度.

So the answer to my question, given the above, and this link I found that setting the size of my Grid, which contained the scrollview and other such fun made it work as advertised. The problem is evidently that the Grid was reporting to the scrollview that it was indefinably big, and so the scrollviewer could be too. So... for my code above, I need to handle when the whole form is resized and set the height of my grid accordingly.

<Grid Name="svMain" Height="550">

</Grid>

如何让 ScrollViewer 在堆栈面板?

这篇关于以编程方式向滚动查看器添加内容,滚动条停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆