0

我正在开发一个 Windows 通用应用程序,我有一个包含 2 个页面的 FlipView,每个页面包含 4 个按钮,当我从第 1 页滚动时,我想要第 2 页,我尝试过这种方式:

<Page.Resources>
<DataTemplate x:Key="FlipViewItemTemplate">
<Grid Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Grid.Column="0" Grid.Row="0" />
<Button Grid.Column="0" Grid.Row="1" />
<Button Grid.Column="1" Grid.Row="0" />
<Button Grid.Column="1" Grid.Row="1" />
</Grid>
</DataTemplate>
<DataTemplate x:Key="FlipViewItemTemplate1">
<Grid Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Grid.Column="0" Grid.Row="0" />
<Button Grid.Column="0" Grid.Row="1" />
<Button Grid.Column="1" Grid.Row="0" />
<Button Grid.Column="1" Grid.Row="1" />
</Grid>
</DataTemplate>
</Page.Resources

我从名为flipView1的flipView中调用了这个方法:

private void flipView1_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
  {
   flipView1.ItemTemplate = Resources["FlipViewItemTemplate"] as DataTemplate;
   }

我得到的是一个有 4 个按钮的页面,没有滚动,有什么方法可以用来在滚动中显示不同的页面

感谢帮助

4

1 回答 1

1

数据模板选择器

可能是使用 DataTemplateSelector 的解决方案

这是一个示例: 1) 该类必须继承自 DataTemplateSelector 名称空间 ExploringOfficeRestAPI.Styles.DataTemplateSelectors { public class FileFolderDataTemplateSelector : DataTemplateSelector { public DataTemplate FileTemplate { get; 放; } 公共数据模板文件夹模板 { 获取;放; }
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container) { var viewModelPublic = item as OneDrivePublicFile; if (viewModelPublic != null) { if (viewModelPublic.IsFile()) { return FileTemplate; }

            return FolderTemplate;
        }          
        return FolderTemplate;

    }
}

} 2) 定义您的 XAML 数据模板: Grid.Row="0" Grid.RowSpan="2" Margin="0" VerticalAlignment="Stretch" Height="150" Width="150"
SelectionHighlightColor="{StaticResource EORAForegroundBrush}" /> Grid.Row="0" Margin="0,0,10,0" VerticalAlignment="Top" Horizo​​ntalAlignment="Right"/> Grid.Row="1" Margin="10,0,0,-20 " 垂直对齐="底部" />

    </Grid>

</DataTemplate>
<DataTemplate x:Key="FileTemplate">
    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="100"/>
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>

        <Image Source="{Binding ThumbnailUrl}" Width="150" Height="150" Margin="0,0,0,0" Grid.RowSpan="2" VerticalAlignment="Top" />
        <TextBlock Text="{Binding name}" Foreground="{StaticResource EORAForegroundBrush}" 
                         Style="{StaticResource EORATextBlockStyle}" Width="auto" Height="50"  
                          Grid.Row="1" Margin="10,0,0,-20" VerticalAlignment="Bottom"  HorizontalAlignment="Left" />

    </Grid>
</DataTemplate>

3) 在 XAMl 中定义选择器: xmlns:selector="using:ExploringOfficeRestAPI.Styles.DataTemplateSelectors"

        <selector:FileFolderDataTemplateSelector 
                    x:Key="FileFolderDataTemplateSelector" 
                    FolderTemplate="{StaticResource FolderTemplate}"
                    FileTemplate="{StaticResource FileTemplate}"/>

4) 最后为您的 FlipView 定义 ItemTemplateSelector="{StaticResource FileFolderDataTemplateSelector}"

于 2015-12-08T13:03:10.377 回答