1

我正在开发一个 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,但它们都显示在同一页面上,所以我没有得到所需的分页......我该如何解决它

4

2 回答 2

2

您的代码将所有项目添加到 StackPanel,然后将该 StackPanel 放入DataPager下方名为“newsStackPanel”的另一个 StackPanel 中。因此,现在,DataPager 与您的新闻文章的显示无关,您不会看到任何分页发生。

相反,请查看此处的 DataPager 示例代码:

http://msdn.microsoft.com/en-us/library/system.windows.controls.datapager(VS.95).aspx#Y9406

您需要修改该示例代码以包含 StackPanel 列表,如下所示:

    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。你在那里有很多选择。也许用名为“newsList”的 ListBox 替换“newsStackPanel”。

好的,这应该足以让您度过难关。

现在做更多的功课: 您应该真正考虑将其切换到 MVVM 模式,在该模式中绑定这些值并将它们模板化,而不是在 C# 中制作 UI 控件。这会产生更简洁的代码,更容易重用,提高可测试性等等。为此,网络上有数百万篇文章。这是来自MS的一个:

http://msdn.microsoft.com/en-us/library/gg430869(v=PandP.40).aspx

于 2011-04-29T23:14:32.100 回答
1

我不知道您使用的 DataPager 控件是否完全处理分页。

您只能将要查看的页面上的新闻项目添加到堆栈面板。

一种简单的方法是在你的 for each 中使用 LINQ,例如:

foreach (Model.News news in s.Skip(newsPager.PageSize * newsPager.PageIndex).Take(newsPager.PageSize))

当页面索引也发生变化时,您必须重新初始化寻呼机中的项目。

于 2011-04-29T00:17:06.380 回答