C# A';装订';无法在';来源';类型为'的属性;装订';

C# A';装订';无法在';来源';类型为'的属性;装订';,c#,wpf,xaml,binding,C#,Wpf,Xaml,Binding,无法在“Binding”类型的“Source”属性上设置“Binding”。只能对DependencyObject的DependencyProperty设置“绑定” <TreeView Height="400" Width="400"> <TreeViewItem ItemsSource="{Binding Source={Binding Path=Data}, XPath=*, Converter={StaticResource stringToXmlDat

无法在“Binding”类型的“Source”属性上设置“Binding”。只能对DependencyObject的DependencyProperty设置“绑定”

    <TreeView Height="400" Width="400">
    <TreeViewItem ItemsSource="{Binding Source={Binding Path=Data}, XPath=*, 
Converter={StaticResource stringToXmlDataProviderConverter},ConverterParameter=/root }" Header="header" />
    </TreeView>

  • ItemsSource=“{Binding Source={Binding Path=Data}有什么问题?

    Data = "<root><parm1>1</parm1><parm2>2</parm2><parm3>3</parm3></root>"
    
    Data=“123”
    
我尝试使用代码示例。 不同之处在于我想将ItemsSource绑定到datacontext中的数据。 转换器没有问题

编辑:

<TreeViewItem ItemsSource="{Binding Path=Data}" Header="Parameters" />

用一个元素(字符串)填充TreeView。因此datacontext是正确的

编辑: 这段代码效果更好。有没有一种在ThreeView中读取XML的通用方法?我不知道XML的结构。在我看到的所有示例中,您都必须声明子节点类型

<TreeViewItem DataContext="{Binding Path=Data, Converter={StaticResource stringToXmlDataProviderConverter}}" ItemsSource="{Binding .}" Header="Parameters" />

无法绑定绑定的属性源,因为它不是依赖项属性。确实不想这样做吗?:

{Binding Path=Data, XPath=*, Converter={StaticResource stringToXmlDataProviderConverter},ConverterParameter=/root}
编辑:

调试绑定时要检查的清单:

1) 您是否正确设置了DataContext

2) 该属性对XAML代码可见吗

3) 属性是否被访问(将断点放入其getter中)。它是否返回它应该返回的值

4) 属性是否已正确更新?必须应用以下任一情况:

  • 该财产为从属财产

  • 该属性通知 其通过inotofyproperty的更改已更改

  • 我们最终确定了财产价值 在调用InitializeComponent()之前


当我使用您的代码时,我没有得到任何异常,但我在树中没有得到任何数据。stringToXmlDataProviderConverter也没有运行。您的datacontext是您期望的吗?{Binding Path=data,Converter={StaticResource stringToXmlDataProviderConverter},ConverterParameter=/root}移除XPath=*会触发转换器,但没有数据。它的getter被访问了吗?它会返回它应该返回的吗?这很奇怪,我看不出getter属性是如何被调用的,但converter没有。你确定转换器吗?数据是什么样子的?它的数据类型是什么?它的属性是什么?