C# 基于xml数据的文本块wpf显示
我正在开发一种存储之类的东西,在那里我阅读RSS提要并在列表框中显示其内容。RSS提要包含其他数据(例如“下载”或“自定义类别”),我使用(或希望使用)这些数据对列表框的结果进行排序。列表框如下所示:C# 基于xml数据的文本块wpf显示,c#,xml,wpf,xaml,listbox,C#,Xml,Wpf,Xaml,Listbox,我正在开发一种存储之类的东西,在那里我阅读RSS提要并在列表框中显示其内容。RSS提要包含其他数据(例如“下载”或“自定义类别”),我使用(或希望使用)这些数据对列表框的结果进行排序。列表框如下所示: <ListBox Grid.Column="2" HorizontalAlignment="Stretch" Name="ItemsListParent" VerticalAlignment="Stretch" Margin="0,25,0,0"> <I
<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>
但是它完成了整个列表框(与之前在wpf代码中一样)而无需重复调用我想为排序添加某种条件,比如if(xmldp.idonknowtheExactName==selectedCategory){显示文本块}或者{不显示它并转到XML中的下一项},但我真的不知道如何做。请对我耐心,因为我是WPF的新手,这也是我的第一个问题。如果你没有真正实现我的目标,下面是一个简单的列表:
1) 我想加载XML并在ItemsList中显示它的所有项
2) 我想在列表框中选择一个名为categoriesList的项目,并基于选择更新项目列表,以仅显示其customCategory==已选择的项目(selected是一个字符串,将根据categoriesList选择进行更新)
问题是,我不知道把条件放在哪里,也不知道它应该是什么样子,甚至不知道它是否可能
希望你能理解,并能帮助我
谢谢你的回答;)Andy好吧,忘了通过代码构建视图吧,WPF不是这样工作的。返回到原始xaml模板 现在的问题是要对
ItemsControl
中的项进行排序和筛选。为此,您需要基于rssFeed将ItemsControl
的ItemsSource
绑定到CollectionView
,而不是绑定到rssFeed本身
CollectionView
允许您轻松对集合进行排序和筛选
顺便说一下,您的XAML中似乎有一个冗余的列表框。它没有做任何事情,因为您在它里面声明了一个ItemsControl
<代码>列表框
已从项控件
派生。
如果您想要滚动条,那么只需使用
列表框
是,如果您确实想要,可以通过代码进行构造,但它破坏了使用WPF的目的,WPF的目的是方便地用XAML表示并绑定到基础数据。它将表示与数据分开,因此如果需要,可以对相同的数据应用完全不同的GUI。了解一下MVVM,它可以很好地保持这种划分。非常感谢,我一定会尝试的!:)
<!-- -->
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);
}