Wpf 将XML文件绑定到UserControl
我想将XML文件中的数据绑定到userControl 到目前为止,我已经: XML文件:Wpf 将XML文件绑定到UserControl,wpf,xml,data-binding,user-controls,Wpf,Xml,Data Binding,User Controls,我想将XML文件中的数据绑定到userControl 到目前为止,我已经: XML文件: <?xml version="1.0" encoding="utf-8"?> <testData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <pumps> <pump>
<?xml version="1.0" encoding="utf-8"?>
<testData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<pumps>
<pump>
<speed value="1200"/>
<color value="Black"/>
<heightAndWidth size="50"/>
</pump>
</pumps>
</testData>
App.xaml
<Application.Resources>
<XmlDataProvider x:Key="testDataDataSource" Source="Desktop\testData.xml" d:IsDataSource="True"/>
</Application.Resources>
用户控制:
<Grid x:Name="LayoutRoot">
<Rectangle x:Name="rect" Fill="{Binding XPath=color}" />
<TextBlock x:Name="line1" Text="{Binding XPath=speed}" />
</Grid>
和MainWindow.xaml
<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource testDataDataSource}}">
<local:RectangleControl DataContext="{Binding Mode=Default, XPath=/testData/pumps/pump}" />
</Grid>
然而,绑定不起作用。您能指出我做错了什么吗。您的XPath没有返回包含您试图绑定到的值的属性。请尝试以下方法:
<Grid x:Name="LayoutRoot">
<Rectangle x:Name="rect" Fill="{Binding XPath=color/@value}" />
<TextBlock x:Name="line1" Text="{Binding XPath=speed/@value}" />
</Grid>
对!我忘了这是一个属性!谢谢