将Combobox绑定到xml文件问题

时间:2011-05-02 09:39:39

标签: wpf xml mvvm binding combobox

这是我的xml

<Courses>

  <Course Name="Computerarchitectuur" />
  <Course Name="Frans" />
  <Course Name="Programmeren" />

</Courses>

在我的xaml中我称之为:

 <XmlDataProvider x:Key="blabla"
                Source="C:\Users\Ruben\Desktop\Stage 26-04\stage_TFW\stage_TFW\NewWPFVragenBeheer\Data\Courses.xml"
                XPath="/Courses/Course"
           />



<ComboBox DataContext="{Binding Source={StaticResource blabla}}"  ItemsSource="{Binding XPath=@Name}" Height="23" HorizontalAlignment="Left" Margin="128,67,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox1_SelectionChanged" />

我的问题是,它只显示第一个元素,即“Computerarchitectuur。是否有人知道我做错了什么?

1 个答案:

答案 0 :(得分:1)

试试这个:

<XmlDataProvider x:Key="blabla" Source="C:\Users\Ruben\Desktop\Stage 26-04\stage_TFW\stage_TFW\NewWPFVragenBeheer\Data\Courses.xml" XPath="Courses/Course" />
<ComboBox ItemsSource="{Binding Source={StaticResource blabla}}" DisplayMemberPath="@Name" Height="23" HorizontalAlignment="Left" Margin="128,67,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox1_SelectionChanged" />