【问题标题】:wpf display of textblock based on xml data基于xml数据的文本块的wpf显示
【发布时间】:2014-02-15 07:57:06
【问题描述】:

我正在开发一种商店或其他东西,我在其中阅读 RSS 提要并在列表框中显示其内容。 RSS 提要包含我使用(或想要使用)对 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) 我想在 ListBox 中选择一个名为 categoriesList 的项目,并根据选择更新 ItemsList 以仅显示其 customCategory==selected 的项目(selected 是一个字符串,将根据 categoriesList 选择进行更新)

问题是,我不知道在哪里放置条件,也不知道它应该是什么样子,甚至是否可能。

希望你能理解并能帮助我。

感谢您的任何回答;)安迪

【问题讨论】:

    标签: c# xml wpf xaml listbox


    【解决方案1】:

    好吧,忘记通过代码构建视图,这不是 WPF 应该如何工作的。回到原来的 xaml 模板。

    现在您遇到的问题是您想要对ItemsControl 中的项目进行排序和过滤。为此,您需要将ItemsControlItemsSource 绑定到基于rssFeed 的CollectionView,而不是绑定到rssFeed 本身。

    CollectionView 允许您轻松地对集合进行排序和过滤。

    顺便说一句,您的 XAML 中似乎有一个多余的 ListBox。它没有做任何事情,因为您在其中声明了 ItemsControlListBox 已经派生自 ItemsControl。 如果您想要滚动条,只需使用ListBox

    【讨论】:

    • 是的,如果您真的愿意,可以通过代码构造,但它违背了使用 WPF 的目标,该目标旨在轻松地用 XAML 表示并绑定到底层数据。它将演示文稿与数据分开,因此您可以根据需要将完全不同的 GUI 应用于相同的数据。阅读 MVVM 可以很好地保持这种划分。
    • 非常感谢,我一定会努力的! :)
    猜你喜欢
    • 2014-05-24
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    相关资源
    最近更新 更多