3
votes

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
Can you explicitly describe what's not working, e.g. is the ScrollViewer refusing to scroll? is it not displaying the items at all? - Fabian Tamp
Stackpanel controls are notorious for messing up scrollbars. I'd suggest for a start to check out the MVVM pattern and how bindings and data templates are applied in WPF rather than attempting to create and assign child controls in code ala WinForms. - Steve Py
@FabianTamp The items load, and they are all there (I can see them if I resize the whole form) but the scroll bar grays out and wont let me scroll. There is not an arrow on the bottom either... which leads me to think it is some sort of property, but I didn't find something that works - iMortalitySX

1 Answers

2
votes

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?