我正在开发一个需要自定义控件的Windows 10应用程序,我需要以下行为,在Windows Phone 10中启用了数据透视控件。
我需要两个部分用于枢轴,例如标题上方的一个部分和标题下方的一个部分。
上面的部分将有一些图像,下面的部分有一个可以滚动的集合。
当我在屏幕上向上滑动时,它应隐藏标题上方的部分。当再次在下面的部分上滑动时,标题下面的内容应该滚动(枢轴被锁定,下面的内容应该滚动)。
当我向下滚动时如果没有要在标题下面滚动的项目,那么标题和标题上方的内容应该会下降(在页面加载时进入初始阶段)。
当我向右/向左滑动时,枢轴和枢轴下方和上方的内容应该更改。
我分享了我的代码 这里..
https://www.dropbox.com/s/vd1vahxdj28qf5g/CustomPivot.zip?dl=0
代码示例 查看MainPage.xaml
<Page
x:Class="PivotControl.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:PivotControl"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<Style x:Key="CustomPivotStyle" TargetType="Pivot">
<Setter Property="Margin" Value="0"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<Grid/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Pivot">
<Grid x:Name="RootElement" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="Orientation">
<VisualState x:Name="Portrait"/>
<VisualState x:Name="Landscape">
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ScrollViewer Grid.Row="1" x:Name="ScrollViewer" VerticalScrollBarVisibility="Auto" ViewChanged="ScrollViewer_ViewChanged"
VerticalScrollMode="Enabled" HorizontalSnapPointsAlignment="Center"
HorizontalSnapPointsType="MandatorySingle" HorizontalScrollBarVisibility="Hidden"
Margin="{TemplateBinding Padding}" Template="{StaticResource ScrollViewerScrollBarlessTemplate}"
VerticalSnapPointsType="None" VerticalContentAlignment="Stretch" ZoomMode="Disabled">
<PivotPanel x:Name="Panel" VerticalAlignment="Stretch" >
<ItemsPresenter x:Name="PivotItemPresenter" Margin="0,-300,0,0">
<ItemsPresenter.RenderTransform >
<TranslateTransform x:Name="ItemsPresenterTranslateTransform" X="0" />
</ItemsPresenter.RenderTransform>
</ItemsPresenter>
<PivotHeaderPanel x:Name="Header" Background="Transparent" Margin="0,300,0,0">
<PivotHeaderPanel.RenderTransform>
<CompositeTransform x:Name="HeaderTranslateTransform" TranslateX="0" />
</PivotHeaderPanel.RenderTransform>
</PivotHeaderPanel>
</PivotPanel>
</ScrollViewer>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate x:Key="GroupTemplate">
<Grid Height="110" Width="500" Margin="0,0,0,10" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="110" Height="110">
<Image Source="{Binding Property3}" Height="110" Width="110"/>
</Border>
<StackPanel Grid.Column="1" Margin="10,0,0,0">
<TextBlock Text="{Binding Property1}" Style="{StaticResource TitleTextBlockStyle}"/>
<TextBlock Text="{Binding Property2}" Style="{StaticResource CaptionTextBlockStyle}" TextWrapping="NoWrap"/>
</StackPanel>
</Grid>
</DataTemplate>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" DataContext="{Binding Source={StaticResource SampleDataSource}}" d:IsLocked="True">
<Pivot Grid.Row="1" x:Name="MainPivot" Style="{StaticResource CustomPivotStyle}" RenderTransformOrigin="0.5,0.5" d:IsLocked="True" >
<Pivot.RenderTransform>
<CompositeTransform/>
</Pivot.RenderTransform>
<Pivot.TitleTemplate>
<DataTemplate >
<TextBlock Visibility="Collapsed"/>
</DataTemplate>
</Pivot.TitleTemplate>
<PivotItem Header="PivotItem 0" Background="{x:Null}" d:IsLocked="True">
<Grid d:IsLocked="True">
<Grid.RowDefinitions>
<RowDefinition Height="250"/>
<RowDefinition Height="75"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Rectangle Fill="#FF7171C3" Stroke="Black" d:IsLocked="True"/>
<ListView Height="430" Loaded="listView_Loaded" x:Name="listView0" HorizontalAlignment="Left" ItemTemplate="{StaticResource GroupTemplate}" ItemsSource="{Binding Groups}" Margin="19,12,19,0" VerticalAlignment="Top" Grid.Row="2" d:IsLocked="True" />
</Grid>
</PivotItem>
<PivotItem Header="PivotItem 1" Background="{x:Null}" d:IsLocked="True">
<Grid d:IsLocked="True" >
<Grid.RowDefinitions>
<RowDefinition Height="250"/>
<RowDefinition Height="75"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Rectangle Fill="#FFB2209E" Stroke="Black" d:IsLocked="True"/>
<ListView Height="430" Loaded="listView1_Loaded" x:Name="listView1" HorizontalAlignment="Left" ItemTemplate="{StaticResource GroupTemplate}" ItemsSource="{Binding Groups}" Margin="19,12,19,0" VerticalAlignment="Top" Grid.Row="2" d:IsLocked="True"/>
</Grid>
</PivotItem>
<PivotItem Header="PivotItem 2" Background="{x:Null}" d:IsLocked="True">
<Grid d:IsLocked="True">
<Grid.RowDefinitions>
<RowDefinition Height="250"/>
<RowDefinition Height="75"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Rectangle Fill="#FF29BF4B" Stroke="Black" d:IsLocked="True"/>
<ListView Height="430" Loaded="listView2_Loaded" x:Name="listView2" HorizontalAlignment="Left" ItemTemplate="{StaticResource GroupTemplate}" ItemsSource="{Binding Groups}" Margin="19,12,19,0" VerticalAlignment="Top" Grid.Row="2" d:IsLocked="True"/>
</Grid>
</PivotItem>
</Pivot>
<!--<Grid ManipulationDelta="Grid_ManipulationDelta" Background="Transparent" x:Name="OverlayGrid" ManipulationMode="TranslateY"/>-->
</Grid>
MainPage.xaml.cs中
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace PivotControl
{
public sealed partial class MainPage : Page
{
private ScrollViewerView FinalView;
double[] ar = new double[2];
bool isUp, isDown;
double initialValue = 0, currentValue;
private ScrollViewerView defaultValue;
private ListView myViewwer;
private List<ScrollViewer> ViewwerList = new List<ScrollViewer>();
public MainPage()
{
this.InitializeComponent(); //
}
private void listView1_Loaded(object sender, RoutedEventArgs e)
{
myViewwer = (sender as ListView);
var data = myViewwer.FindChild<ScrollViewer>();
data.VerticalScrollMode = ScrollMode.Disabled;
data.ViewChanged += Data_ViewChanged;
ViewwerList.Add(data);
}
private void listView2_Loaded(object sender, RoutedEventArgs e)
{
myViewwer = (sender as ListView);
var data = myViewwer.FindChild<ScrollViewer>();
data.VerticalScrollMode = ScrollMode.Disabled;
data.ViewChanged += Data_ViewChanged;
ViewwerList.Add(data);
}
ScrollViewer mainScrollViewer;
private void ScrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
if (mainScrollViewer == null)
mainScrollViewer = sender as ScrollViewer;
currentValue = (sender as ScrollViewer).VerticalOffset;
var maxVerticalOffset = mainScrollViewer.ScrollableHeight;
Debug.WriteLine(currentValue + " ***" + initialValue + " c-1 " + (initialValue - currentValue));
if (initialValue - currentValue < 0 && !isUp)
{
//AnimateUp.Begin();
mainScrollViewer.ViewChanged -= ScrollViewer_ViewChanged;
(sender as ScrollViewer).ChangeView((sender as ScrollViewer).HorizontalOffset, 300, 1);
isUp = true;
isDown = false;
mainScrollViewer.VerticalScrollMode = ScrollMode.Disabled;
//mainScrollViewer.Visibility = Visibility.Collapsed;
//mainScrollViewer.HorizontalScrollMode = ScrollMode.Disabled;
// mainScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
foreach (var item in ViewwerList)
{
item.VerticalScrollMode = ScrollMode.Enabled;
//item.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
}
//initialValue = 250;
}
else if (initialValue - currentValue > 10 && !isDown)
{
// AnimateDown.Begin();
//(sender as ScrollViewer).ChangeView((sender as ScrollViewer).HorizontalOffset, 0, 1);
isUp = false;
isDown = true;
mainScrollViewer.VerticalScrollMode = ScrollMode.Enabled;
mainScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
// mainScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
foreach (var item in ViewwerList)
{
item.VerticalScrollMode = ScrollMode.Disabled;
//item.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
}
}
initialValue = currentValue;
}
private void listView_Loaded(object sender, RoutedEventArgs e)
{
myViewwer = (sender as ListView);
var data = myViewwer.FindChild<ScrollViewer>();
data.VerticalScrollMode = ScrollMode.Disabled;
//data.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
data.ViewChanged += Data_ViewChanged;
ViewwerList.Add(data);
}
}
}
}
任何帮助表示赞赏。