在我正在处理的Windows手机项目中,我需要有一个页面,其中包含来自同一类的项目列表,它应该显示为类似
图片 - 文字 文字 - 图片 图像 - 文字 文字 - 图片 图像 - 文字 文字 - 图片
<ScrollViewer Name="NoticiasScroll" Grid.Row="1" Margin="0,38,0,0">
<ItemsControl x:Name="items" Height="653">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Height="218" VerticalAlignment="Top" Background="White" Width="440" Margin="0,0,0,5">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Image Name="NewsImg" Source="/Assets/Images/pipa.png" Stretch="Fill" Grid.Column="0"/>
<Grid Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="3*" />
</Grid.RowDefinitions>
<TextBlock TextAlignment="Right" Text="{Binding titulo}" Foreground="Black" VerticalAlignment="Top" Height="39" Width="200" Margin="0,10,10,0" FontSize="28" FontFamily="Helvetica" HorizontalAlignment="Right"/>
<TextBlock TextAlignment="Right" Grid.Row="1" Text="{Binding texto}" Foreground="Black" FontFamily="Helvetica" FontSize="18" TextWrapping="Wrap" Margin="22,0,8,0" Width="190"/>
</Grid>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
现在我有这个,但这样所有项目都以相同的格式显示。我怎样才能使它们在两种格式之间交替?
答案 0 :(得分:1)
您可以使用2个可见性属性扩展绑定到ItemsControl的类。
public class MyItem
{
public Visibility ShowEven { get; set; }
public Visibility ShowOdd { get; set; }
public string Titulo { get; set; }
public string Texto { get; set; }
}
设定交替的可见性:
for (int i = 0; i < myItems.Count; i++)
{
bool isEven = i % 2 == 0;
myItems[i].ShowEven = isEven ? Visibility.Visible : Visibility.Collapsed;
myItems[i].ShowOdd = isEven ? Visibility.Collapsed : Visibility.Visible;
}
items.ItemsSource = myItems;
在ItemTemplate中使用绑定到可见性属性的2个网格。
<ItemsControl Name="items">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid Visibility="{Binding ShowEven}" Background="Blue">
<Image />
<Image />
</Grid>
<Grid Visibility="{Binding ShowOdd}" Background="Red">
<TextBlock />
<TextBlock />
</Grid>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>