我正在开发一种商店或其他东西,我在其中阅读RSS Feed并在ListBox中显示其内容。 RSS源包含我使用(或想要使用)对ListBox的结果进行排序的其他数据(例如“download”或“customCategory”)。 ListBox看起来像这样:
<ListBox Grid.Column="2" HorizontalAlignment="Stretch" Name="ItemsListParent" VerticalAlignment="Stretch" Margin="0,25,0,0">
<ItemsControl Name="ItemsList" ItemsSource="{Binding Source={StaticResource rssData}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Name="itemElement" Orientation="Horizontal" Loaded="itemElement_Loaded">
<StackPanel.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="White" Offset="0.15" />
<GradientStop Color="LightGray" Offset="0.85" />
<GradientStop Color="White" Offset="1" />
</LinearGradientBrush>
</StackPanel.Background>
<!--<Image Width="15" Margin="2" Source="{Binding XPath=url, Path=InnerText}"/>-->
<!--<TextBlock Margin="2" FontSize="16" VerticalAlignment="Center" Text="{Binding XPath=title}" FontWeight="Normal">
<Hyperlink Name="lnkGoToArticle" Tag="{Binding XPath=link, Path=InnerText}" Click="lnkGoToArticle_Click">
>>
</Hyperlink>
<Button Name="lnkDownload" Tag="{Binding XPath=download, Path=InnerText}" Style="{DynamicResource NoChromeButton}" Click="lnkDownload_Click">
<Image Source="Images/download31.png" Name="DownloadIcon" Width="30" Height="30" />
</Button>
</TextBlock>-->
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ListBox>
中的代码
<!-- -->
是我重写为C#的内容,因为我认为我可以弄清楚如何对XML进行排序。关键是,我已经创建了
void UpdateListBox()
{
ItemsList.Items.Clear();
selected = (string)CategoriesList.SelectedItem;
//if (selected==xmldp
/*System.Xml.XmlDocument data = new System.Xml.XmlDocument();
data.Load(@"http://www.andystore.bluefile.cz/?feed=rss2");
xmldp.Document = data;
xmldp.XPath = "//item";*/
Thickness mrg = new Thickness();
mrg.Left = 2;
mrg.Right = 2;
mrg.Top = 2;
mrg.Bottom = 2;
TextBlock itemTitle=new TextBlock();
itemTitle.Margin=mrg;
itemTitle.FontSize=16;
itemTitle.VerticalAlignment = VerticalAlignment.Center;
itemTitle.Text = "{Binding XPath=title}";
itemTitle.FontWeight = FontWeights.Normal;
itemTitle.Name="itemTitle";
Binding itemTitleBinding=new Binding();
itemTitleBinding.XPath="title";
itemTitle.SetBinding(TextBlock.TextProperty,itemTitleBinding);
itemElement.Children.Add(itemTitle);
itemElement.RegisterName(itemTitle.Name, itemTitle);
Label gta = new Label();
Hyperlink goToArticle = new Hyperlink();
goToArticle.Click += new RoutedEventHandler(lnkGoToArticle_Click);
goToArticle.Inlines.Add(@">>");
Binding goToArticleBinding = new Binding();
goToArticleBinding.Path = new PropertyPath("InnerText");
goToArticleBinding.XPath = "link";
goToArticle.SetBinding(Hyperlink.TagProperty, goToArticleBinding);
gta.Content = goToArticle;
itemElement.Children.Add(gta);
itemElement.RegisterName(goToArticle.Name, goToArticle);
Button downloadButton = new Button();
downloadButton.Name = "lnkDownload";
downloadButton.Cursor = Cursors.Hand;
downloadButton.Click += new RoutedEventHandler(lnkDownload_Click);
Binding downloadButtonBinding = new Binding();
downloadButtonBinding.XPath = "download";
downloadButtonBinding.Path = new PropertyPath("InnerText");
downloadButton.SetBinding(Button.TagProperty, downloadButtonBinding);
Style downloadButtonStyle = this.FindResource("NoChromeButton") as Style;
downloadButton.Style = downloadButtonStyle;
BitmapImage dbiBitmap = new BitmapImage();
dbiBitmap.BeginInit();
dbiBitmap.UriSource = new Uri("pack://application:,,,/AndyLaunchWPF;component/Images/download31.png");
dbiBitmap.EndInit();
Image dbi = new Image();
dbi.Width = 30;
dbi.Height = 30;
dbi.Name = "downloadIcon";
dbi.Source = dbiBitmap;
downloadButton.Content = dbi;
itemElement.Children.Add(downloadButton);
itemElement.RegisterName(downloadButton.Name, downloadButton);
//itemElement.Children.Add(dbi);
//itemElement.RegisterName(dbi.Name, dbi);
}
但它完成整个列表框(如前所述在wpf代码中)而不重复调用!! 我想添加一些条件进行排序,例如if(xmldp.IDontKnowTheExactName == selectedCategory){显示文本块} else {不显示它并转到XML中的下一项}但我真的不知道该怎么做。请耐心等待我,因为我是WPF的新手,这也是我的第一个问题。如果您没有真正得到我想要实现的目标,这里有一个简单的列表:
1)我想加载XML并在ItemsList
中显示它的所有项目2)我想在ListBox中选择一个名为categoriesList的项目,并根据选择更新ItemsList来仅显示其customCategory ==选中的项目(选择的是一个将根据categoriesList选择更新的字符串)< / p>
问题是,我不知道在哪里放置条件,也不知道它应该是什么样子以及它是否可能。
希望你理解,你能帮助我。
感谢您的回答;)Andy
答案 0 :(得分:0)
确定忘记通过代码构建视图,这不是WPF应该如何工作的。返回原始的xaml模板。
现在您遇到的问题是您要对ItemsControl
中的项目进行排序和过滤。为此,您需要根据rssFeed将ItemsSource
的{{1}}绑定到ItemsControl
,而不是绑定到rssFeed本身。
CollectionView
可让您轻松对集合进行排序和过滤。
顺便说一下,您的XAML中似乎有一个冗余的CollectionView
。由于你在其中声明了ListBox
,所以它没有做任何事情。 ItemsControl
已经来自ListBox
。
如果您需要滚动条,请使用ItemsControl
。