如何在数据透视控件UWP上启用拖放

时间:2019-02-25 20:44:21

标签: c# xaml uwp

我真的很喜欢Pivot Control,以及如何在触摸屏上的页面之间滑动。它的响应就像Tab控件一样,并且效果很好。我的问题是我想拖放以对透视项进行重新排序。任何帮助将不胜感激。这是我为数据透视表准备的代码。

<Pivot x:Name="PivotMain" CanDrag="True" AllowDrop="True" FontFamily="Segoe UI" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,0,0" BorderBrush="Black"  >
                <Pivot.HeaderTemplate>
                    <DataTemplate>
                        <Grid Margin="0,0,0,0">
                            <TextBlock Text="{Binding}" FontSize="13" Margin="0,0,0,0" />
                        </Grid>
                    </DataTemplate>
                </Pivot.HeaderTemplate>
                <PivotItem x:Name="Main" Header="Home" CanDrag="True" FontSize="11" Margin="10,10,10,10" FontFamily="Segoe UI" HorizontalAlignment="Stretch">
                    <WebView x:Name="Home" DefaultBackgroundColor="Transparent" NavigationCompleted="NavigationCompleted"  NewWindowRequested="NewWindowRequested" VerticalAlignment="Stretch" />
                </PivotItem>
            </Pivot>

1 个答案:

答案 0 :(得分:0)

Pivot控件继承自ItemsControl,但不继承自ListViewBase。因此它不包含CanDragItems CanReorderItems属性,您无法像ListView那样对它们重新排序。 Pivot具有ItemsSource属性,这意味着您可以绑定ObservableCollection。并使用重新排序数据源对其进行重新排序。

<Pivot
    x:Name="PivotMain"
    Margin="0,0,0,0"
    HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch"
    AllowDrop="True"
    BorderBrush="Black"
    FontFamily="Segoe UI"
    >
    <Pivot.ItemTemplate>
        <DataTemplate>
            <PivotItem
                CanDrag="True"
                FontFamily="Segoe UI"
                FontSize="11"
                Header="{Binding}"
                >
                <Frame />
            </PivotItem>
        </DataTemplate>
    </Pivot.ItemTemplate>
    <Pivot.HeaderTemplate>
        <DataTemplate>
            <Grid Margin="0,0,0,0">
                <TextBlock
                    Margin="0,0,0,0"
                    FontSize="13"
                    Text="{Binding}"
                    />
            </Grid>
        </DataTemplate>
    </Pivot.HeaderTemplate>
</Pivot>

背后的代码

 ObservableCollection<string> Items;
 public MainPage()
 {
     Items = new ObservableCollection<string>() { "first", "second", "third", "forth" };
     PivotMain.ItemsSource = Items;
     this.InitializeComponent();
 }

重新订购

var _timer = new Timer(async _ =>
{
    Random r = new Random();
    var randomIndex = r.Next(0, Items.Count - 1);
    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
        Items.Insert(4, Items[randomIndex]);
        Items.RemoveAt(randomIndex);
    });
}
, null, 0, 1000);