Ciprian Jijie
Ciprian Jijie

Reputation: 477

How to add a scrollViewer in to a stack panel for a text block?

I try to add an scroll Viewer for a text block who was created from behind c#, text block was added to a stack panel stackPanel.Children.Add(text block). I want to do that in Windows Phone 8.0.

When make something like that:

StackPanel stackPanel = new StackPanel();    
ScrollViewer sv = new ScrollViewer();    
sv.Content = stackPanel;

I receive:

ExceptionObject = {"Value does not fall within the expected range."}.

One solution to solve that exception?

Upvotes: 0

Views: 277

Answers (2)

Schiriac Robert
Schiriac Robert

Reputation: 91

ScrollViewer calculates it's scrollbars based on dimensions of child controls. If your TextBlock has Height property set, remove it and ScrollBars should work as expected.

Also you should set:

sv.Content = yourTextBlock;

Upvotes: 2

Mikael Koskinen
Mikael Koskinen

Reputation: 12906

With the following code (where Content is Grid):

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        var textBlock = new TextBlock() { Text = "hello" };

        var stackPanel = new StackPanel();
        stackPanel.Children.Add(textBlock);

        var sv = new ScrollViewer { Content = stackPanel };

        this.Content.Children.Add(sv);
    }

I get the desired output:

Manual textbox

So I tried to reproduce your error. I get the same exception if TextBlock is null. So maybe your code which creates the TextBlock has some issues? Here's an example:

        TextBlock text = null;

        var stackPanel = new StackPanel();
        stackPanel.Children.Add(text);

        var sv = new ScrollViewer { Content = stackPanel };

        this.Content.Children.Add(sv);

Will result in:

Exception

Upvotes: 1

Related Questions