简体   繁体   English

datapager silverlight中的stackpanel

[英]stackpanel in datapager silverlight

I am developing a silverlight navigation application and got stuck on the following problem... 我正在开发Silverlight导航应用程序,但遇到以下问题:

The guy I am developing the app wants to have a News page where you can see all published news on the left side and the clicked (or latest news if none is clicked) on the right side. 我正在开发应用程序的家伙希望有一个“新闻”页面,您可以在左侧看到所有已发布的新闻,而在右侧可以看到被单击的新闻(如果没有单击,则可以看到最新的新闻)。 He wanted to have a header, text and publishing date for each news in the news list. 他想为新闻列表中的每个新闻提供标题,文本和发布日期。 Also he wanted to have paging so that there won't be to many news in the list at once... 他还想进行分页,以便列表中不会有太多新闻。

I did this: 我这样做:

        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;

and all of it goes here 所有的东西都在这里

<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>

The newsPager displayes (correctly) 2 pages because i have currently 5 news and the pageSize is set to 3 but they are all displayed on the same page so I dont get the desired paging... how can i fix it newsPager显示(正确)2页,因为我当前有5条新闻并且pageSize设置为3,但是它们都显示在同一页上,所以我没有得到想要的分页...我该如何解决它

Your code is adding all the items to a StackPanel, then it is putting that StackPanel inside another StackPanel called "newsStackPanel" below the DataPager. 您的代码将所有项目添加到StackPanel中,然后将其放置在DataPager 下方另一个名为“ newsStackPanel”的StackPanel中。 So, right now, the DataPager has nothing to do with the display of your news articles, and you won't be seeing any paging happening. 因此,现在,DataPager与新闻报道的显示无关,并且您不会看到任何分页发生。

Instead, take a look at the DataPager sample code here: 相反,请在此处查看DataPager示例代码:

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

You'll need to modify that sample code to contain a list of StackPanels like this: 您需要修改该示例代码以包含如下所示的StackPanels列表:

    List<StackPanel> itemList = new List<StackPanel>();

Then, for each of your news items, add them to that list instead of an outer StackPanel: 然后,对于您的每个新闻项目,将它们添加到该列表中,而不是外部StackPanel:

    itemList.Add(stackPanel);

You'll then wrap that up and bind it to both your data pager and a new list control : 然后,将其包装起来,并将其绑定到数据分页器和新的列表控件

    // 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;

The sample uses a ListBox called "listBox1". 该示例使用一个名为“ listBox1”的ListBox。 You have lots of choices there. 您在那里有很多选择。 Perhaps replace the "newsStackPanel" with a ListBox called "newsList". 也许用一个名为“ newsList”的列表框替换“ newsStackPanel”。

OK, that should be enough to get you through this. 好的,那应该足以帮助您解决这个问题。

Now for a little more homework: You should really consider switching this to the MVVM pattern where you bind these values and template them instead of making UI controls in C#. 现在要做更多功课:您应该真正考虑将其切换到MVVM模式,在其中绑定这些值并对其进行模板化,而不是在C#中创建UI控件。 This results in much cleaner code, enables much easier reuse, improves testability, and so on. 这样可以使代码更简洁,更容易重用,提高可测试性等等。 There are a million zillion articles on the web for this. 网上有上百万的文章。 Here is one from MS: 这是来自MS的一个:

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

I don't know if the DataPager control you're using handles the paging completely. 我不知道您使用的DataPager控件是否可以完全处理分页。

You could only add the news items that are on the page you want to view to the stack panel. 您只能将要查看的页面上的新闻项添加到堆栈面板。

One easy way to do that could be to use LINQ in your for each, something like: 一种简单的方法是在每个对象中使用LINQ,例如:

foreach (Model.News news in s.Skip(newsPager.PageSize * newsPager.PageIndex).Take(newsPager.PageSize)) foreach(s.Skip中的Model.News新闻(newsPager.PageSize * newsPager.PageIndex).Take(newsPager.PageSize))

you'd have to reinitialize the items in the pager when the page index changes then too. 当页面索引更改时,您也必须重新初始化寻呼机中的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM