Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 绑定到ObservableCollection的列表上的数据模板的Xpath查询_C#_Wpf_Xml - Fatal编程技术网

C# 绑定到ObservableCollection的列表上的数据模板的Xpath查询

C# 绑定到ObservableCollection的列表上的数据模板的Xpath查询,c#,wpf,xml,C#,Wpf,Xml,所以我在WPF中玩一些异步的东西,我试图加载一个组合框,其中包含一些从堆栈溢出提要中提取的XML数据,我得到的数据没有问题,我可以看到组合框正在填充,但我似乎无法获得文章的标题。我尝试了一些方法,但事实上我只是没有太多地使用XML数据,不知道我在做什么 这是我的数据模板 <ComboBox.ItemTemplate> <DataTemplate> <Border Background="Azure" Margin="1" Height="24" Widt

所以我在WPF中玩一些异步的东西,我试图加载一个组合框,其中包含一些从堆栈溢出提要中提取的XML数据,我得到的数据没有问题,我可以看到组合框正在填充,但我似乎无法获得文章的标题。我尝试了一些方法,但事实上我只是没有太多地使用XML数据,不知道我在做什么

这是我的数据模板

<ComboBox.ItemTemplate>
  <DataTemplate>
    <Border Background="Azure" Margin="1" Height="24" Width="100">
      <Label Content="{Binding XPath=InnerXml/title}" />
    </Border>
  </DataTemplate>
</ComboBox.ItemTemplate>
正如您所看到的,我正在尝试获取InnerXML/title—这不起作用,我已经尝试了其他几种方法。这里是我填充列表的地方

private async Task LoadListAsync(string url, int sleep)
    {
        Task<XmlNodeList> task = Task.Factory.StartNew(() =>
        {
            Thread.Sleep(sleep);
            return GetFeed(url);
        });

        Log("In LoadListAsync before await");

        var list = await task;
        foreach (XmlNode node in list)
        {
            List.Add(node);
        }

        Log("In LoadListAsync, after await"); 
    }
正如我所说的,我可以看到foreach循环开始,列表开始填充——我知道这一点,因为我可以从模板中看到边界,我似乎无法通过手工和XPath查询来获取标题

有人能帮忙吗?在过去的任何时候,我都会在blend中使用XML数据源,而不是ObserverableCollection,这与此有关吗

谢谢

这是XML结构——我可以在绑定SelectedXml的文本框中看到它

<id xmlns=\"http://www.w3.org/2005/Atom\">http://stackoverflow.com/q/8872600</id><re:rank scheme=\"http://stackoverflow.com\" xmlns:re=\"http://purl.org/atompub/rank/1.0\">0</re:rank><title type=\"text\" xmlns=\"http://www.w3.org/2005/Atom\">
Which is faster to use binding using converter or update in a property changed callback?</title>
<category scheme=\"http://stackoverflow.com/feeds/tag?tagnames=wpf/tags\" term=\"wpf\" xmlns=\"http://www.w3.org/2005/Atom\" />
<category scheme=\"http://stackoverflow.com/feeds/tag?tagnames=wpf/tags\" term=\"binding\" xmlns=\"http://www.w3.org/2005/Atom\" />
<category scheme=\"http://stackoverflow.com/feeds/tag?tagnames=wpf/tags\" term=\"delegates\" xmlns=\"http://www.w3.org/2005/Atom\" />
<category scheme=\"http://stackoverflow.com/feeds/tag?tagnames=wpf/tags\" term=\"callback\" xmlns=\"http://www.w3.org/2005/Atom\" />
<category scheme=\"http://stackoverflow.com/feeds/tag?tagnames=wpf/tags\" term=\"propertychanged\" xmlns=\"http://www.w3.org/2005/Atom\" />
<author xmlns=\"http://www.w3.org/2005/Atom\"><name>mihajlv</name><uri>http://stackoverflow.com/users/906042</uri></author>
<link rel=\"alternate\" href=\"http://stackoverflow.com/questions/8872600/which-is-faster-to-use-binding-using-converter-or-update-in-a-property-changed-c\" 
xmlns=\"http://www.w3.org/2005/Atom\" />
<published xmlns=\"http://www.w3.org/2005/Atom\">2012-01-15T19:23:34Z</published>
<updated xmlns=\"http://www.w3.org/2005/Atom\">2012-01-15T19:23:34Z</updated>
<summary type=\"html\" xmlns=\"http://www.w3.org/2005/Atom\">\r\n            &lt;p&gt;I need fast updates, so I was wondering which one is faster and more efficient.&lt;/p&gt;\n\r\n        </summary>
InnerXml是XML节点的元属性,您不能使用XPath访问它,如果节点具有直接子元素title,那么XPath=atom:title就足够了

请注意,需要有一个名称空间,即使将atom名称空间设置为默认名称空间,XPath查询也需要限定元素

具体例子:


啊哼。。。我绑定到一个ObservableCollecion,当然我不能在其中使用XPath查询,就WPF而言,它是一个对象,就像我绑定到一个人员集合、项目集合或其他对象一样

我需要创建一个转换器,我没有绑定到XML,而是绑定到一个对象XmlNode。这是转换器

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var node = (XmlNode)value;
         if (node["title"].InnerText == null)
                return "(no title)";
            return node["title"].InnerText;
    }
这是正确的装订

<Label Content="{Binding Converter={StaticResource XmlNodeToTitleStringConverter}}" />

就XML结构而言,节点是什么样子的?这就是为什么我对InnerXml的值是这样感到困惑的原因。。。那么标题是XML结构中的一个标记?您是否可以提供节点模式的示例,以便我们提出更好的建议?是的,这是我第一次尝试,Xpath=title,但当这不起作用时,我被派去钓鱼,嗯?它显然有一个标题,我知道这是因为我已经查看了aw XML。@Kenn:您仍然应该提供节点的完整XML,并查看是否有。我知道没有绑定错误-我只是将IsSynchronizedWithCurrentItem设置为True,我可以看到XML正在设置,我甚至创建了一个名为XmlString的属性,返回SelectedXmlNode.InnerXml,并将其绑定到一个文本框中,该文本框可以正常工作。我唯一的问题是在combox box datatemplate中对其进行XPath查询,我不知道我做错了什么。@Kenn:没有XML结构,人们也帮不了你。很明显,将被破坏的绑定是标题绑定,而不是所有其他东西,请检查错误。我在主要帖子中放了一些示例XML,以便您可以看到它的结构。谢谢,你错了。如果你仍然不相信我,你可以试试我在回答中添加的例子。
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var node = (XmlNode)value;
         if (node["title"].InnerText == null)
                return "(no title)";
            return node["title"].InnerText;
    }
<Label Content="{Binding Converter={StaticResource XmlNodeToTitleStringConverter}}" />