SelectionChanged和FlipView

时间:2015-12-07 12:27:48

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

我正在开发一个Windows通用应用程序,我有一个有2个页面的FlipView,每个页面包含4个按钮,我想当我从page1滚动时我得到page2,我试过这样:

<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个按钮而没有滚动的页面,是否可以使用任何方法在滚动中显示不同的页面

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

DataTemplateSelector

可能是使用DataTemplateSelector

的解决方案

这是一个示例: 1)该类必须继承自DataTemplateSelector nanamespace ExploringOfficeRestAPI.Styles.DataTemplateSelectors {     公共类FileFolderDataTemplateSelector:DataTemplateSelector     {         public DataTemplate FileTemplate {get;组; }         public DataTemplate FolderTemplate {get;组; }
        protected override DataTemplate SelectTemplateCore(object item,DependencyObject container)         {             var viewModelPublic = item作为OneDrivePublicFile;             if(viewModelPublic!= null)             {                 if(viewModelPublic.IsFile())                 {                     返回FileTemplate;                 }

            return FolderTemplate;
        }          
        return FolderTemplate;

    }
}

} 2)定义您的XAML DataTemplate:                                                                                                                        Grid.Row =“0”Grid.RowSpan =“2”Margin =“0”VerticalAlignment =“Stretch”                                高度=“150”宽度=“150”
                               SelectionHighlightColor =“{StaticResource EORAForegroundBrush}”/&gt;                                            Grid.Row =“0”Margin =“0,0,10,0”VerticalAlignment =“Top”Horizo​​ntalAlignment =“Right”/&gt;                                            Grid.Row =“1”Margin =“10,0,0,-20”VerticalAlignment =“Bottom”/&gt;

    </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:选择= “使用:ExploringOfficeRestAPI.Styles.DataTemplateSelectors”

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

4)最后为你的FlipView定义ItemTemplateSelector =“{StaticResource FileFolderDataTemplateSelector}”