[英]wpf display of textblock based on xml data
我正在開發一種商店或其他東西,在其中閱讀RSS Feed並將其內容顯示在ListBox中。 RSS Feed包含我用於(或想要使用)對ListBox的結果進行排序的其他數據(例如“下載”或“ 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){顯示文本塊}否則{不顯示它並轉到XML的下一個項目},但我真的不知道該怎么做。 請耐心等待,因為我是WPF的新手,這也是我的第一個問題。 如果您還沒有真正實現我想要達到的目標,那么這里是一個簡單的清單:
1)我想加載XML並在ItemsList中顯示所有項目
2)我想在列表框中選擇一個名為CategoriesList的項目,並基於選擇更新ItemsList以僅顯示具有其customCategory == selected的項目(選擇的字符串將根據categoryList的選擇進行更新)
問題是,我不知道放置條件的位置,也不知道它的外觀以及是否可能。
希望您能理解並且能夠幫助我。
謝謝你的回答;)安迪
好吧,忘記通過代碼構造視圖,這不是WPF應該工作的方式。 返回到原始的xaml模板。
現在,您遇到的問題是您想對ItemsControl
的項目進行排序和過濾。 為此,您需要根據rssFeed將ItemsControl
的ItemsSource
綁定到CollectionView
,而不是綁定到rssFeed本身。
CollectionView
使您可以輕松地對集合進行排序和過濾。
順便說一下,您的XAML中似乎有一個冗余的ListBox
。 它沒有做任何事情,因為您要在其中聲明ItemsControl
。 ListBox
已經從ItemsControl
派生。 如果需要滾動條,則只需使用ListBox
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.