SelectionChanged 和 FlipView

SelectionChanged and a FlipView

我正在开发一个 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个按钮的页面,有什么方法可以让我在滚动时显示不同的页面

感谢帮助

DataTemplateSelector

可能是使用 DataTemplateSelector 的解决方案

这是一个示例: 1) class 必须继承自 DataTemplateSelector 命名空间 ExploringOfficeRestAPI.Styles.DataTemplateSelectors { public class 文件文件夹数据模板选择器:数据模板选择器 { public DataTemplate FileTemplate { 得到;放; } public DataTemplate 文件夹模板{ get;放; }
受保护的覆盖 DataTemplate SelectTemplateCore(对象项,DependencyObject 容器) { var viewModelPublic = 项目作为 OneDrivePublicFile; 如果(viewModelPublic != null) { 如果 (viewModelPublic.IsFile()) { return 文件模板; }

            return FolderTemplate;
        }          
        return FolderTemplate;

    }
}

} 2) 定义您的 XAML DataTemplate : Grid.Row="0" Grid.RowSpan="2" Margin="0" VerticalAlignment="Stretch" 高度="150" 宽度="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" VerticalAlignment="Bottom" />

    </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}"