How can I apply an effect to a ListView where the top 30px graduate from fully transparent to fully opaque? The idea is that as you scroll down, the top items gradually fade away.
I am building a UWP application where the design calls for the top 30px of a ListView to start at opacity 0 and transition to opacity 1. Conceptually I am imagining an Opacity Mask that would be applied to the top part of a SpriteVisual but I cannot work out how to achieve this.
I am attempting this using the the anniversary edition of Windows 10, Composition and Win2D.
Edit: a picture may paint a 1000 words:
If you look at this image, there are two content elements in the bottom-left and bottom-right. Although the background appears to be black, it is actually a gradient. If you examine the top of the two elements, they become more transparent towards the top, showing through the background. That is the effect I am trying to achieve.
Edit 2: In an attempt to show the outcome of the effect I am looking for, here is a GIF that shows the effect if I use overlaid bitmaps:
The header background image is:
The lower 30px has an alpha gradient and appears above the gridview giving the apparent effect of the grid view items fading out and sliding undernext the background.
The XAML layout looks like:
<Page
x:Class="App14.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App14"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="150" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Image Source="/Assets/background.png"
Grid.Row="0"
Grid.RowSpan="2"
VerticalAlignment="Top"
Stretch="None" />
<GridView Grid.Row="1"
Margin="96,-30,96,96">
<GridView.Resources>
<Style TargetType="Image">
<Setter Property="Height" Value="400" />
<Setter Property="Width" Value="300" />
<Setter Property="Margin" Value="30" />
</Style>
</GridView.Resources>
<Image Source="Assets/1.jpg" />
<Image Source="Assets/2.jpg" />
<Image Source="Assets/3.jpg" />
<Image Source="Assets/4.jpg" />
<Image Source="Assets/5.jpg" />
<Image Source="Assets/6.jpg" />
<Image Source="Assets/7.jpg" />
<Image Source="Assets/8.jpg" />
<Image Source="Assets/9.jpg" />
<Image Source="Assets/10.jpg" />
<Image Source="Assets/11.jpg" />
<Image Source="Assets/12.jpg" />
</GridView>
<!-- Header above content -->
<Image Grid.Row="0" Source="/Assets/header_background.png"
Stretch="None" />
<TextBlock x:Name="Title"
Grid.Row="0"
FontSize="48"
Text="This Is A Title"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Foreground="White" />
</Grid>
I think one thing we should know is that by default
ListView
control's background is transparent. So ifListView
's parent control is set a image as background, to achieve your desired layout, we need set another background forListView
, and meanwhile this background can't fill the wholeListView
.So, here is a method:
As you can see in this code, I set a image as background for the rootGrid, and put another
Grid
inside to achieve your desired layout. In this Grid, of course theListView
should take all the space, but we can separate thisGrid
into two parts, one part is forLinearGradientBrush
, and the other one is for background of theListView
. Here is the rendering image of this layout:And if you want to set another image as background for
ListView
, I think we can only get the average color of this image and bind theGradientStop
ofOffset = 1
to this color.Update
For the foreground of the
ListView
, I think you are right, we need to cover a mask over it. Here is a method:There is a problem here, by default the scrollbar of the
ListView
can be seen and when using a mask over it, the scrollbar will also be covered. To achieve a better layout, it's better to setScrollViewer.VerticalScrollBarVisibility="Hidden"
to theListView
.So with some assistance from @sohcatt on the Windows UI Dev Labs issues list, I have built a working solution.
Here is the XAML:
Here is the code:
The code above creates a 30px gradient that goes updown from total whiteness to total transparency. Try to put it on your listview and see if it goes well.