2011-12-02 9 views
1

これがなぜ機能しないのかは誰にも分かります。 これは本当にシンプルですが、ListBoxは起動時に空です。コードの背後にはInitializeComponent()だけが含まれています。ListBoxをXmlDataProviderにバインドする

うまくいけば、誰かがアイデア... H.B @

<Window x:Class="DasDataGrid.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:sys="clr-namespace:System;assembly=mscorlib" 
    Title="MainWindow" Height="350" Width="700"> 

    <Window.Resources> 
     <XmlDataProvider x:Key="Maschinen" XPath="/machines"> 
      <x:XData> 
       <machines> 
        <machine name="alte Maschine"/> 
        <machine name="neue Maschine"/> 
       </machines> 
      </x:XData> 
     </XmlDataProvider> 
    </Window.Resources> 
    <ListBox ItemsSource="{Binding Source={StaticResource Maschinen},XPath=machine/@name}" 
       IsSynchronizedWithCurrentItem="True" 
       SelectedIndex="1"> 
    </ListBox> 
</Window> 

を持っています。 ここに私がテストしたコードがあります。それを起動すると、ListBoxはまだ空です。私は何が間違っているのか分からない。

あなたは空の文字列にするのxmlnsを設定する必要が
<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<StackPanel> 
    <StackPanel.Resources> 
     <XmlDataProvider x:Key="Maschinen"> 
     <x:XData> 
      <machines xmlns=""> 
       <machine name="alte Maschine"/> 
       <machine name="neue Maschine"/> 
      </machines> 
     </x:XData> 
     </XmlDataProvider> 
    </StackPanel.Resources> 

    <ListBox ItemsSource="{Binding Source={StaticResource Maschinen}, XPath=machine}" 
     IsSynchronizedWithCurrentItem="True" DisplayMemberPath="@name" 
     SelectedIndex="1"> 
    </ListBox> 

</StackPanel> 
</Window>  

答えて

0

:XMLデータのルートノードは、XML名前空間を設定のxmlns属性を持つ

<x:XData> 
    <machines xmlns=""> 
     <machine name="alte Maschine"/> 
     <machine name="neue Maschine"/> 
    </machines> 
</x:XData> 

MSDN空の文字列に変換します。これは、XAMLページ内のインラインであるデータアイランドにXPathクエリを適用するための要件です。このインラインの場合、XAML、したがってデータアイランドは、System.Windows名前空間を継承します。このため、XPathクエリがSystem.Windows名前空間によって修飾されないようにするためには、名前空間を空白にする必要があります。


そして、あなたはthuslyバインドする場合があります(それは結果の面で違いはありませんが):

<ListBox ItemsSource="{Binding Source={StaticResource Maschinen}, XPath=machine}" 
      IsSynchronizedWithCurrentItem="True" DisplayMemberPath="@name" 
      SelectedIndex="1"> 
</ListBox> 
+0

はありがとうございます。私はあなたのコードをテストしましたが、うまくいきません。 ListBoxはまだ空です。何か案は? – user1078325

+0

私はこのことをテストしました。編集したコードを一時的に質問に追加できますか? –

+1

私は何が間違っているのか分かりました:XPath =/machines/machine それはうまくいきます! ありがとう! – user1078325

関連する問題