Now that ScrollView ContentSize property is readonly how do I SET
the size of the ScrollView content?
That very same property that is settable in UIScrollView
(but not in xamarin forms)
I have this:
<ScrollView Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" HorizontalOptions="FillAndExpand"
x:Name="attachmentScroller"
>
<StackLayout x:Name="AttachmentsView" Orientation="Horizontal" VerticalOptions="Fill"/>
</ScrollView>
what I've tried is setting AttachmentsView.MinimumWidthRequest to the desired contentWidth;
But that does not seem to expand the parent's scrollview content width
to the desired value.
You can set the set of the content size, by setting the property of the content, instead of the the ScrollView.
Here's it in xaml
<ScrollView>
<StackLayout HeightRequest="100" />
</ScrollView>
To stop the scroll view restricting the size of the contained content to its own size, ensure you set
ScrollView Orientation="Horizontal" or "Both" or "Vertical" respectively.
i.e. for your example -
<ScrollView Orientation="Horizontal" Grid.Row="1"
Grid.Column="0" Grid.ColumnSpan="3"
HorizontalOptions="FillAndExpand"
x:Name="attachmentScroller">
<StackLayout WidthRequest="1000" x:Name="AttachmentsView"
Orientation="Horizontal" VerticalOptions="Fill"/>
</ScrollView>
Related
I'm trying to implement a CarouselView to display a set of images on my app. Apparently, I can't remove the Whitespace at the Bottom of the CarouselView no matter what combinations I tried.
I've placed BackgroundColor property for CarouselView [Red], Grid[Green] (inside DataTemplate), and IndicatorView [Blue] to see which of them is consuming the rest of the screen despite the lack of StackLayout and it seems that either CarouselView or Grid is causing the unwanted behaviour.
Here's my XAML Code with roughly nothing on the ViewModel but a Mock Database for the Image Collection:
<ContentPage.Content>
<Grid ColumnDefinitions="*"
RowDefinitions="Auto,Auto,Auto,1*">
<Label Grid.Row="0" Grid.Column="0"
Text="CarouselView Test"
TextColor="Black"
FontAttributes="Bold"
FontSize="20"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
Padding="10" />
<CarouselView Grid.Row="1" Grid.Column="0" BackgroundColor="Red" HeightRequest="{Binding ScreenWidth}"
x:Name="TheCarousel"
ItemsSource="{Binding ImageSourceCollection}"
IndicatorView="indicatorView">
<CarouselView.ItemTemplate>
<DataTemplate>
<Grid RowDefinitions="Auto" ColumnDefinitions="Auto" BackgroundColor="Green" HorizontalOptions="Center" VerticalOptions="Center">
<Image Grid.Row="0" Grid.Column="0" Source="{Binding .}" />
</Grid>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
<IndicatorView Grid.Row="2" Grid.Column="0" BackgroundColor="Blue"
x:Name="indicatorView"
IndicatorColor="LightGray"
IndicatorSize="10"
SelectedIndicatorColor="Black" />
</Grid>
</ContentPage.Content>
And here's a screenshot of my current build with the CarouselView/Grid consuming most of the Screen Space:
CarouselView has some weird behaviors on size calculating,in your scenario the only way is get the height it(template) needs in code(viewmodel), and bind it to the HeightRequest on CarouselView .
Xaml
<CarouselView HeightRequest="{Binding xxx}"
View model
public double xxx {
get
{
// calculate the height according to the width by ratio
return height;
}
}
I think you need to set Space between rows and columns to Zero if you want to make your indicatorView stick to your CarouselView
So what actually worked for me.
I set Grid Row Definition to ex.(300) and then Carousel Height Request to 350.
I'm setting Image Width based on Screen Width, but I tried it Manually ex (400) and still worked, don't get confused.
<Grid RowDefinitions="300">
<CarouselView x:Name="carouselView"
Grid.Row="0"
ItemsSource="{Binding HomeCarousel}">
<CarouselView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Frame HasShadow="False"
CornerRadius="0"
Margin="0"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"
HeightRequest="350">
<SwipeView>
<Image Source="{Binding ImageSource}"
Aspect="AspectFill"
WidthRequest="{DynamicResource FullScreenWidth}"
HorizontalOptions="Center" />
</SwipeView>
</Frame>
</StackLayout>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
</Grid>
i have this:
<StackLayout VerticalOptions="FillAndExpand">
<LineEntry />
<ScrollView VerticalOptions="FillAndExpand">
<StackLayout x:Name="stack" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
</StackLayout>
</ScrollView>
</StackLayout>
But it does not scroll, except if you are into the LineEntry, because the keyboard is up. I think it's because the content page has less height than the scrollView, but don't know how to fix this.
I will update some mockups(can't use the real images) for more information, in the first one, does not works, in the second one, it works:
the content page has less height than the scrollView
This should be the first reason , and the send reason is the Content should exists some things , not a empty layot in ScrollView :
The follow code can work , just for reference :
<StackLayout VerticalOptions="FillAndExpand">
<Entry Text="First Entry"/>
<ScrollView HeightRequest="100" BackgroundColor="Beige" VerticalScrollBarVisibility="Always">
<StackLayout x:Name="stack"
HorizontalOptions="FillAndExpand"
HeightRequest="200">
<Entry Placeholder="Second Entry"/>
</StackLayout>
</ScrollView>
</StackLayout>
In Xamarin Forms 4.1, When scroll, scrollview content is covering/overflow the headerview and scrollview content is not shown completely.
Tried with IsClippedToBounds to true.
Created scrollviewrenderer, OnDraw() -> set clip to bound property. But didn't worked.
Example: Here is the XAML design
<Stacklayout>
<HeaderView/>
<ScrollView IsClippedToBounds="true">
<Stacklayout>
<label text="1"/>
........
........
<label text="100"/>
</Stacklayout>
</ScrollView>
</Stacklayout>
I make a custom control as the headerview in your description. And test on Xamarin.forms 4.1, works well.
MyCustomConrol:
<StackLayout Orientation="Horizontal" BackgroundColor="Aqua">
<Label x:Name="Title_label" Text="Title" TextColor="Black" FontSize="25" HorizontalOptions="StartAndExpand" Margin="8,0,4,0"/>
<Button x:Name="btnHello" Text="Hello" HorizontalOptions="EndAndExpand" Margin="4,0,8,0"/>
</StackLayout>
MainPage.xaml
<StackLayout>
<local:MyCustomControl></local:MyCustomControl>
<!--<HeaderView/>-->
<ScrollView IsClippedToBounds="true">
<StackLayout>
<Label Text="1"/>
<Label Text="2"/>
<Label Text="3"/>
……………
…………………
</StackLayout>
</ScrollView>
Result:
You could download from ScrollView/XamarinDemo folder of GitHub for reference.
https://github.com/WendyZang/Test.git
If you want your header to be always visible then rather than placinf your content in Header, it will be better to place your header content first then below that place your listview. Thus, your header remains intact and listview scrolls.
I have an image slider and I want to position a search box on top of the image slider.
Here is my code:
<StackLayout HeightRequest="200" >
<cv:CarouselView Margin="0" ItemsSource="{Binding Zoos}" x:Name="CarouselZoos">
<cv:CarouselView.ItemTemplate>
<DataTemplate>
<Image Grid.RowSpan="1" Grid.Row="0" Aspect="AspectFill" Source="{Binding ImageUrl}"/>
</DataTemplate>
</cv:CarouselView.ItemTemplate>
</cv:CarouselView>
</StackLayout>
<StackLayout>
<SearchBar FontAttributes="Bold"
FontFamily="Raleway.ttf#Raleway"
FontSize="11"
Placeholder="SEARCH FOR MEDICINE AND PRODUCTS"
HorizontalOptions="Center" VerticalOptions="CenterAndExpand"
Focused="SearchBar_Focused"/>
</StackLayout>
The easiest way to do this is to wrap your element into a grid and placing the second one into the same grid.
The grid layout will put all elements inside the same cell over each other with the last one being added laying topmost.
<Grid>
<Image ... />
<Label VerticalOptions="Center" HorizontalOptions="Center" />
</Grid>
This example would put the label in the mid over the image.
When using interactive elements, keep in mind that they have to be the last ones added in order to be clickable.
So for instance, if you had
<Grid>
<Button ... />
<Image ... />
</Grid>
the button would end up being overlayed by the image and therefore not be clickable.
I have a listview in a stacklayout. If the listview is shorter than the height of the stacklayout's container, stacklayout's height should wrap its content. If it is longer, stacklayout will have its max. height and the listview will scroll.
In short, I want to assign a non-static max height to the stacklayout but the following gives it its max. height even though the list is short:
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="Center" BackgroundColor="White">
<Label Text="Hello" FontAttributes="Bold"></Label>
<ListView HasUnevenRows="True" ItemsSource="{Binding options}" VerticalOptions="Start">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding title}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
Is there a way to accomplish this?