C# datapager silverlight中的stackpanel
我正在开发silverlight导航应用程序,遇到以下问题 我正在开发应用程序的那个家伙希望有一个新闻页面,你可以在左侧看到所有已发布的新闻,在右侧看到已点击的新闻(或最新新闻,如果没有点击)。他希望新闻列表中的每一条新闻都有一个标题、文本和发布日期。他还想进行寻呼,这样列表中就不会同时出现太多的新闻 我这样做:C# datapager silverlight中的stackpanel,c#,.net,silverlight,datapager,C#,.net,Silverlight,Datapager,我正在开发silverlight导航应用程序,遇到以下问题 我正在开发应用程序的那个家伙希望有一个新闻页面,你可以在左侧看到所有已发布的新闻,在右侧看到已点击的新闻(或最新新闻,如果没有点击)。他希望新闻列表中的每一条新闻都有一个标题、文本和发布日期。他还想进行寻呼,这样列表中就不会同时出现太多的新闻 我这样做: foreach (Model.News news in s) { StackPanel stackPanel = new Sta
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,但它们都显示在同一页上,所以我无法获得所需的页面。。。如何修复它我不知道您使用的DataPager控件是否能完全处理分页 您只能将要查看的页面上的新闻项添加到堆栈面板 一种简单的方法是在for each中使用LINQ,例如: foreach(s.Skip中的Model.News新闻(newsPager.PageSize*newsPager.PageIndex).Take(newsPager.PageSize))
当页面索引也发生变化时,您必须重新初始化寻呼机中的项目。您的代码将所有项目添加到StackPanel中,然后将该StackPanel放入DataPager下方另一个名为“newsStackPanel”的StackPanel中。所以,现在,DataPager与您的新闻文章的显示无关,您将不会看到任何分页发生 相反,请在此处查看DataPager示例代码: 您需要修改该示例代码以包含如下StackPanel列表:
List<StackPanel> itemList = new List<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”的列表框。你有很多选择。也许可以将“newsStackPanel”替换为一个名为“newsList”的列表框
好的,这应该足以让你度过难关
现在再做一点家庭作业:
你真的应该考虑把它转换成MVVM模式,在这些模式中绑定这些值并对它们进行模板化,而不是在C++中使用UI控件。这将导致更干净的代码,更容易重用,提高可测试性,等等。网上有无数关于这方面的文章。这是微软的一个例子:
仍然存在问题,如果我使用列表框,我就得不到我想要的样式。。。。但是詹姆斯解决了这个问题。。无论如何,非常感谢。。。是的,我很快就会把它全部换成MVVM。。。这是该应用程序的第一个版本,很快将进行全面检查:)没问题,很高兴我能提供帮助。
// 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;