我正在开发一个silverlight导航应用程序,并被以下问题卡住了…
我正在开发的应用程序的家伙想要有一个新闻页面,在那里你可以看到所有已发布的新闻在左侧和点击(或最新的新闻,如果没有点击)在右侧。他希望新闻列表中的每个新闻都有标题、文本和发布日期。此外,他还希望有分页功能,这样列表中就不会同时有太多新闻……
我这样做了:
foreach (Model.News news in s)
{
StackPanel stackPanel = new StackPanel();
HyperlinkButton hyperlinkButton = new HyperlinkButton();
hyperlinkButton.Tag = news.Header;
hyperlinkButton.Content = news.Header;
hyperlinkButton.FontSize = 15;
hyperlinkButton.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
hyperlinkButton.Click += new RoutedEventHandler(Button_Click);
stackPanel.Children.Add(hyperlinkButton);
TextBlock textBlock = new TextBlock();
textBlock.Foreground = new SolidColorBrush(Colors.Gray);
textBlock.FontSize = 12;
textBlock.FontFamily = new FontFamily("Verdana");
textBlock.TextWrapping = TextWrapping.Wrap;
textBlock.Text = news.Text;
stackPanel.Children.Add(textBlock);
TextBlock dateTextBlock = new TextBlock();
dateTextBlock.Foreground = new SolidColorBrush(Colors.Gray);
dateTextBlock.FontSize = 10;
dateTextBlock.FontFamily = new FontFamily("Verdana");
dateTextBlock.TextWrapping = TextWrapping.Wrap;
dateTextBlock.FontWeight = FontWeights.Bold;
dateTextBlock.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
dateTextBlock.Text = news.Date.ToShortDateString();
stackPanel.Children.Add(dateTextBlock);
stackPanel.Children.Add(new TextBlock());
newsStackPanel.Children.Add(stackPanel);
}
PagedCollectionView itemListView = new PagedCollectionView(newsStackPanel.Children);
newsPager.Source = itemListView;所有的一切都在这里
<Grid x:Name="LayoutRoot" Loaded="LayoutRoot_Loaded" MaxWidth="1100">
<Grid.RenderTransform>
<CompositeTransform/>
</Grid.RenderTransform>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="2"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<RichTextBox Name="contentRTB" MaxWidth="1000" Margin="10, 30, 10, 30" Grid.Column="2"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
TextWrapping="Wrap"
Style="{StaticResource RichTextBoxStyle}" IsReadOnly="True"/>
<Rectangle Grid.Column="1" Margin="0,10"
Fill="#FF0067C6"/>
<TextBlock Name="header" Foreground="#FF0067C6" FontSize="18" FontFamily="Verdana" HorizontalAlignment="Center" VerticalAlignment="Top" Grid.Column="0"></TextBlock>
<sdk:DataPager x:Name="newsPager"
DisplayMode="FirstLastNumeric"
Background="#FF0067C6"
PageSize="3"
AutoEllipsis="True"
NumericButtonCount="3"/>
<StackPanel Name="newsStackPanel" Grid.Column="0" Orientation="Vertical" Margin="0,50,0,0"/>
</Grid>newsPager (正确地)显示2页,因为我当前有5条新闻,并且pageSize设置为3,但是它们都显示在同一页上,所以我没有得到所需的分页……我怎么才能修复它呢?
发布于 2011-04-29 08:17:06
我不知道您正在使用的DataPager控件是否完全处理分页。
只能将要查看的页面上的新闻项添加到堆栈面板中。
一种简单的方法是在for each中使用LINQ,如下所示:
newsPager.PageIndex).Take(newsPager.PageSize)) (s.Skip中的Model.News新闻(newsPager.PageSize* foreach
当页面索引发生变化时,您也必须重新初始化分页程序中的项。
发布于 2011-04-30 07:14:32
您的代码将所有项添加到一个StackPanel中,然后将该StackPanel放入DataPager下另一个名为"newsStackPanel“的StackPanel中。因此,现在,DataPager与新闻文章的显示没有任何关系,您将不会看到任何分页发生。
取而代之的是这里的DataPager示例代码:
http://msdn.microsoft.com/en-us/library/system.windows.controls.datapager(VS.95).aspx#Y9406
您需要修改该示例代码,使其包含如下所示的StackPanels列表:
List<StackPanel> itemList = new List<StackPanel>();然后,对于您的每个新闻项目,将它们添加到该列表中,而不是外部StackPanel:
itemList.Add(stackPanel);然后将其打包并将其绑定到数据分页程序和一个新的列表控件
// Wrap the itemList in a PagedCollectionView for paging functionality
PagedCollectionView itemListView = new PagedCollectionView(itemList);
// Set the DataPager and ListBox to the same data source.
newsPager.Source = itemListView;
listBox1.ItemsSource = itemListView;该示例使用名为"listBox1“的ListBox。你有很多选择。也许可以将"newsStackPanel“替换为名为"newsList”的ListBox。
好了,这应该足以让你度过难关了。
你真的应该考虑切换到模式,在这个模式中你绑定这些值并将它们模板化,而不是在C#中创建UI控件。这使得代码更加整洁,使得重用更加容易,提高了可测试性,等等。网上有一百万篇关于这方面的文章。下面是微软的一个例子:
http://msdn.microsoft.com/en-us/library/gg430869(v=PandP.40).aspx
https://stackoverflow.com/questions/5713863
复制相似问题