我想在我的代码中添加一个flipview,就像这张图片一样,
但是当我在每个页面中滑动时,我会在所有页面中得到此结果:
这是我的代码,
<FlipView x:Name="flipView1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="642">
<FlipView.ItemTemplate>
<DataTemplate>
<Grid Style="{Binding HorizontalAlignment, ElementName=grid}" ScrollViewer.HorizontalScrollBarVisibility="Auto" Background="Transparent" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Style="{Binding HorizontalAlignment, ElementName=grid}" ScrollViewer.HorizontalScrollBarVisibility="Auto" Background="Transparent" Margin="110,85,10,195" Grid.Row="0" Grid.Column="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Grid.Column="0" Grid.Row="0" Background="#2c3389" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="22,24,25,10" />
<Button Grid.Column="1" Grid.Row="0" Background="#2c3389" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="22,24,25,10"/>
<Button Grid.Column="0" Grid.Row="1" Background="#2c3389" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="22,0,25,27" />
<Button Grid.Column="1" Grid.Row="1" Background="#2c3389" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="22,0,25,27" />
</Grid>
<Grid Style="{Binding HorizontalAlignment, ElementName=grid}" ScrollViewer.HorizontalScrollBarVisibility="Auto" Background="Transparent" Margin="70,85,45,195" Grid.Row="0" Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Grid.Column="0" Grid.Row="0" Background="#2c3389" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="22,24,25,10" />
<Button Grid.Column="1" Grid.Row="0" Background="#2c3389" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="22,24,25,10"/>
<Button Grid.Column="0" Grid.Row="1" Background="#2c3389" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="22,7,25,10" />
<Button Grid.Column="1" Grid.Row="1" Background="#2c3389" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="22,7,25,10" />
</Grid>
</Grid>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>
请您知道如何更正我的代码并在这种情况下使用数据绑定,我在一个通用的Windows应用程序上工作 谢谢你的帮助
答案 0 :(得分:1)
之所以发生这种情况,是因为您在ItemTemplate
中将2x类似的网格彼此靠近。 ItemTemplate
定义每个项目的视图(在本例中为FlipView的每个页面)。如果您将设置FlipView的ItemsSource
,则此集合中的每个项目都将完全复制ItemTemplate。您可以像这样修复您的XAML:
<DataTemplate>
<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" Background="#2c3389" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="22,24,25,10" />
<Button Grid.Column="1" Grid.Row="0" Background="#2c3389" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="22,24,25,10" />
<Button Grid.Column="0" Grid.Row="1" Background="#2c3389" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="22,0,25,27" />
<Button Grid.Column="1" Grid.Row="1" Background="#2c3389" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="22,0,25,27" />
</Grid>
</DataTemplate>
我删除了一个带有按钮和外部网格的网格。现在一个页面将包含4个按钮。
您可以为FlipView设置ItemsSource
。 flipView1.ItemsSource = /* collection of models */
@edit
如果要在Image
中使用绑定并设置Button.Content
,则需要包含单个FlipView项目中每个按钮的图像源的类。例如:
public class ButtonImages
{
public string Image1 { get; set; }
public string Image2 { get; set; }
public string Image3 { get; set; }
public string Image4 { get; set; }
}
然后你应该用这个类的对象准备集合。
var flipViewPages = new List<ButtonImages>();
\\ prepare your collection - set Image1, Image2.. properties and add these objects to collection
flipView1.ItemSource = flipViewPages
然后在XAML中,您可以使用{Binding}
<Button Grid.Column="0" Grid.Row="0" Background="#2c3389" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="22,24,25,10">
<Image Source="{Binding Image1}" />
</Button>
如果您需要有关绑定的更多信息,请在其他帖子和网站上查找。 https://msdn.microsoft.com/library/ms752347(v=vs.100).aspx