我有我的ListView的麻煩。我的數據模板由圖像和2個文本塊組成。但有些圖像沒有源代碼,因此不會顯示。我認爲閃爍的原因是有些項目沒有圖像(只有文本塊),所以它們有不同的大小。那我該如何解決呢?ListView標題在滾動時閃爍
這是我的XAML代碼:
<Page.Resources>
<CollectionViewSource x:Name="phts" IsSourceGrouped="True"/>
<DataTemplate x:Key="AddrBookItemTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Source="{Binding Image}" Name="image" MaxHeight="90" MaxWidth="90" Stretch="Fill" Tag="{Binding Url}"/>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Name="txt1" Text="{Binding Title}" Tag="{Binding Url}"/>
<TextBlock Grid.Row="1" Name="txt2" Text="{Binding Title2}" Tag="{Binding Url}"/>
</Grid>
</Grid>
</DataTemplate>
<DataTemplate x:Key="AddrBookGroupHeaderTemplate">
<Border Background="Transparent" Margin="0,5,0,5" Tag="{Binding Key}">
<Border Background="#E0E0E0"
Width="400" Height="30" Margin="0,0,0,0" HorizontalAlignment="Left" Tag="{Binding Group_ID}">
<TextBlock Text="{Binding Key}" Foreground="Black" FontSize="18" Padding="6"
FontFamily="{StaticResource PhoneFontFamilySemiLight}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</Border>
</Border>
</DataTemplate>
</Page.Resources>
<Grid x:Name="grid">
<ListView Background="White"
Foreground="Black"
ItemsSource="{Binding Source={StaticResource phts}}"
ItemTemplate="{StaticResource AddrBookItemTemplate}">
<ListView.GroupStyle>
<GroupStyle HidesIfEmpty="True" HeaderTemplate="{StaticResource AddrBookGroupHeaderTemplate}"/>
</ListView.GroupStyle>
</ListView>
</Grid>
這是我的代碼:
public sealed partial class TestPage : Page
{
public TestPage()
{
this.ManipulationMode = ManipulationModes.All;
this.InitializeComponent();
loadContent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void loadContent()
{
NewsGroup group1 = new NewsGroup("Test 1");
NewsGroup group2 = new NewsGroup("Test 2");
NewsGroup group3 = new NewsGroup("Test 3");
NewsGroup group4 = new NewsGroup("Test 4");
NewsGroup group5 = new NewsGroup("Test 5");
NewsGroup group6 = new NewsGroup("Test 6");
List<NewsGroup> groups = new List<NewsGroup>(new NewsGroup[] {group1, group2, group3, group4, group5, group6});
Random rand = new Random();
foreach(NewsGroup group in groups)
{
for (int i = 0; i < 10; i++)
{
ArticleHeader hdr = new ArticleHeader();
if (rand.Next() % 5 == 2)
{
hdr.Image = "https://pbs.twimg.com/profile_images/1885929594/Nomad_watermark_white.jpg";
}
hdr.Title = "Short title";
hdr.Title2 = "Very big title here should be. Bla bla bla bla bla! Bla bla bla? Blablabl blab bla bla lab bbalblablalb blablalbal";
group.Add(hdr);
}
}
((CollectionViewSource)Resources["phts"]).Source = groups;
}
}
public class ArticleHeader
{
public string Time { get; set; }
public string Title { get; set; }
public string Url { get; set; }
public string Image { get; set; }
public string Title2 { get; set; }
}
public class NewsGroup : List<ArticleHeader>
{
public NewsGroup(string name)
{
Key = name;
}
public string Key { get; set; }
public string GroupUrl { get; set; }
public string Group_ID { get; set; }
}
在ColumnDefinition中使用*大小而不是Auto。 – Vishal 2014-08-31 15:10:30
@Vishal,那不能解決問題 – FruitDealer 2014-08-31 15:14:40
可以請你發佈圖片嗎? – Vishal 2014-08-31 15:19:30