wpf无法将组合框绑定到XML文件

时间:2018-04-12 18:55:45

标签: xml wpf combobox

我是WPF的新手,我正在尝试将XML文件绑定到组合框控件。这是我的XML,我已将其添加到我的项目中,其名称为AvailableTestsXMLFile2.xml ...

<AvailableTests>
  <Test name="high voltage" testNumber="1" />
  <Test name="low voltage" testNumber="2" />
  <Test name="new test" testNumber="3" />
  <Test name="old test" testNumber="4" />
</AvailableTests>

这是我的XAML ......

<Window x:Class="WpfTester.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfTester"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
    <XmlDataProvider x:Key="AvailableTests" Source="AvailableTestsXMLFile2.xml" XPath="AvailableTests"/>   
    <StackPanel Orientation="Vertical" MaxWidth="500" HorizontalAlignment="Left">
        <Grid>
            <Image HorizontalAlignment="Left" VerticalAlignment="Top" Source="C:\Users\StarkS02\Pictures\abbott-logo.jpg" Height="42" Width="42"/>
        </Grid>

        <Border Background="DeepSkyBlue" BorderThickness="2" BorderBrush="Black" CornerRadius="10" Margin="10" Width="450" Height="55">
            <StackPanel>
                <TextBlock>Configuration Section</TextBlock>
            </StackPanel>

        </Border>
        <Border Background="DeepSkyBlue" BorderThickness="2" BorderBrush="Black" CornerRadius="10"  Margin="10">
            <StackPanel>
                <TextBlock>Select Test</TextBlock>
                <ComboBox x:Name="employeeNameBox" IsReadOnly="False" HorizontalAlignment="Left" IsEditable="False" ItemsSource="{Binding Source={StaticResource AvailableTests}, XPath=./Test/name}">
            </ComboBox>                    
                <CheckBox Name="ChkRepeatTilFailure" Foreground="White" Content="Repeat Test Until Failure" Margin="5 5 5 5"/>
                <Button Content="Submit Test" Margin="4 4 4 4" Width="65"/>

            </StackPanel>

        </Border>
        <Border Background="DeepSkyBlue" BorderThickness="2" BorderBrush="Black" CornerRadius="10" Margin="10" Height="65">

            <StackPanel>
                <TextBlock>Test Response Section</TextBlock>
                <TextBox Text="Test 3 Successful!" />
            </StackPanel>
        </Border>
    </StackPanel>
</Window>

运行时没有任何编译器错误,但我的组合框中没有任何值。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

ComboBox的itemsource绑定中没有定义任何源。纠正这个。

ComboBox Binding to XML in WPF

答案 1 :(得分:0)

您的itemssource绑定错误 您需要使用xml作为源 仔细比较你对此有何看法: https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-bind-to-xml-data-using-an-xmldataprovider-and-xpath-queries