Forum Xamarin.Forms

ScrollView within ViewCell?

MaruMaru DEMember ✭✭
edited June 2014 in Xamarin.Forms

Hi everyone,

is it possible to have a ScrollView inside of a ViewCell?
I have a ListView with following code inside of ItemTemplate:

return new ViewCell
{
    View = new StackLayout
    {
        Padding = new Thickness(0, 5),
        Orientation = StackOrientation.Horizontal,
        Children = 
        {
            switcher,
            scrollView
        }
    }
};

switcher is a Switch and scrollView a ScrollView with Content = new Label { Text = "This is a Text" }.

With this I get every time when I try to add my ListView to my Page a "ScrollView can host only one direct child" exception. As stated above I only set one Label to the Content of the ScrollView.

Best Answer

Answers

  • MaruMaru DEMember ✭✭
    edited August 2014

    Just to say what I want to accomplish: the Label next to the Switch should be scrollable when the text is longer than the screen.

    Rest of the code:

    ItemTemplate = new DataTemplate(() =>
    {
    
    LabelFont label = new LabelFont
    {
        FontSize = 20,
        HorizontalOptions = LayoutOptions.Start
    };
    label.SetBinding(Label.TextProperty, "name");
    
    ScrollView scrollView = new ScrollView
    {
        Content = label
    };
    
    Switch switcher = new Switch();
    switcher.HorizontalOptions = LayoutOptions.Start;
    switcher.SetBinding(Switch.IsToggledProperty, "IsSubscriped");
    
    return new ViewCell
    {
        View = new StackLayout
        {
            Padding = new Thickness(0, 5),
            Orientation = StackOrientation.Horizontal,
            Children = 
            {
                switcher,
                scrollView
            }
        }
    };
    })
    
  • MaruMaru DEMember ✭✭

    push

  • MitchMilamMitchMilam USMember ✭✭✭

    Instead of scrolling, have you considered enabling WordWrap on the label?

  • MaruMaru DEMember ✭✭

    @MitchMilam‌ Thanks for the tip! I was looking into it but can't find where to set WordWrap. Can you give me a short sample? :)

  • MitchMilamMitchMilam USMember ✭✭✭

    how about this:

                    new Label
                    {
                        TextColor = Color.Black,
                        Text = "This is a very long string.",
                        LineBreakMode = LineBreakMode.WordWrap
                    }
    
  • MaruMaru DEMember ✭✭

    @MitchMilam‌ Ah, thanks again!

    Sadly the words still stay in the same line and going outside of the screen. :/

  • MaruMaru DEMember ✭✭

    @MitchMilam‌ Haha, not really proud of that! :D Thanks for filling a bug report.

  • MitchMilamMitchMilam USMember ✭✭✭

    @Maru, I tried everything I could think of to make that work, but ran out of time and options. It should work just the way you designed it.

    This is not the first word-wrap issue I've found so maybe there is a trend.

Sign In or Register to comment.