stackpanel in datapager silverlight
本文关键字:silverlight datapager in stackpanel | 更新日期: 2023-09-27 17:51:22
我正在开发一个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,但它们都显示在同一页上,所以我没有得到所需的分页…
你的代码是添加所有的项目到一个StackPanel,然后它是把StackPanel内的另一个StackPanel称为"newsStackPanel" 低于的DataPager。因此,现在,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。你有很多选择。或许可以将"newsStackPanel"替换为一个名为"newsList"的ListBox。
好的,这些应该足够你度过这段时间了。
现在再做一点作业:你真的应该考虑切换到MVVM模式,在那里你绑定这些值并将它们模板化,而不是在c#中制作UI控件。这将产生更干净的代码,更容易重用,提高可测试性,等等。网上有无数关于这个的文章。以下是来自MS的一条:
http://msdn.microsoft.com/en-us/library/gg430869 (v = PandP.40) . aspx
我不知道你正在使用的DataPager控件是否完全处理分页。
您只能将想要查看的页面上的新闻项添加到堆栈面板。
一个简单的方法是在你的每一个中使用LINQ,像这样:
foreach(模型。英文新闻,英文新闻。PageSize * newspager . pagindex).Take(newsPager.PageSize))
当页面索引也发生变化时,您必须重新初始化页导航中的项。