【问题标题】:SelectionChanged and a FlipViewSelectionChanged 和 FlipView
【发布时间】:2015-12-08 21:03:10
【问题描述】:

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

感谢帮助

【问题讨论】:

  • 这在通用应用中可行吗?

标签: c# xaml win-universal-app windows-10-universal


【解决方案1】:

DataTemplateSelector

可能是使用 DataTemplateSelector 的解决方案

这是一个示例: 1) 类必须继承自 DataTemplateSelector 名称空间 ExploringOfficeRestAPI.Styles.DataTemplateSelectors { 公共类 FileFolderDataTemplateSelector : DataTemplateSelector { 公共数据模板文件模板 { 获取;放; } 公共数据模板文件夹模板 { 获取;放; }
受保护的覆盖 DataTemplate SelectTemplateCore(对象项,DependencyObject 容器) { var viewModelPublic = 作为 OneDrivePublicFile 的项目; 如果(viewModelPublic!= null) { if (viewModelPublic.IsFile()) { 返回文件模板; }

            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="使用:ExploringOfficeRestAPI.Styles.DataTemplateSelectors"

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多