2011-04-19 78 views
1

我正在開發一個Silverlight導航應用程序,並就死在了下面的問題......的StackPanel在DataPager的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,但它們都顯示在同一頁面上,所以我沒有得到所需的分頁......我該如何修復它

回答

1

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

您只能將要查看的頁面上的新聞項添加到堆棧面板。

一個簡單的方法來做到這一點可能是使用LINQ你的每一個,像這樣:

的foreach(在s.Skip(newsPager.PageSize * newsPager.PageIndex)。取Model.News消息(newsPager .PageSize))

當頁面索引發生變化時,您必須重新初始化尋呼機中的項目。

+0

這就是我一直在尋找...非常感謝:) – 2011-05-01 20:25:14

+0

沒問題,很高興我能幫忙。 – 2011-05-02 00:49:08

2

您的代碼將所有項目添加到StackPanel,然後它將該StackPanel放入另一個名爲「newsStackPanel」的DataPanel下方的DataPager的。所以,現在,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中」列表框。你有很多選擇。也許用名爲「newsList」的列表框替換「newsStackPanel」。

好吧,那應該足以讓你度過這個難關。

現在多一點功課: 你真的應該考慮這個切換到你綁定這些值MVVM模式和模板而不是在C#使UI控件他們。這會產生更簡潔的代碼,使重用更容易,提高可測試性等等。網絡上有一百萬個文章。這是一個從MS:

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

+0

仍然有問題,那麼如果我使用列表框,我沒有得到我想要的樣式....但詹姆斯解決了它..非常感謝... ...是的,我很快切換到MVVM ...這是該應用程序的第一個版本將很快得到改進:) – 2011-05-01 20:27:17