Programmatically adding content to scrollviewer, scrollbar stops working

折月煮酒 提交于 2019-12-24 06:27:59

问题


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.

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
        });
    }

回答1:


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).

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>

How can I get ScrollViewer to work inside a StackPanel?



来源:https://stackoverflow.com/questions/14347066/programmatically-adding-content-to-scrollviewer-scrollbar-stops-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!