次の方法で共有


方法 : 階層 XML データでマスタ詳細パターンを使用する

更新 : 2007 年 11 月

この例では、XML データでマスタ詳細シナリオを実装する方法を示します。

使用例

この例は、「方法 : 階層データでマスタ詳細パターンを使用する」で説明した例の XML データ バージョンです。この例では、データは League.xml ファイルから取得します。3 番目の ListBox コントロールが、2 番目の ListBoxSelectedValue プロパティにバインドすることにより、その選択項目の変更を追跡していることに注意してください。

<Window x:Class="SDKSample.Window1"
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  Title="Multiple ListBox Binding Sample"
  Width="400" Height="200"
  Background="Cornsilk">
    <Window.Resources>
      <XmlDataProvider x:Key="MyList" Source="Data\Leagues.xml"
                       XPath="Leagues/League"/>
      <DataTemplate x:Key="dataTemplate">
        <TextBlock Text="{Binding XPath=@name}" />
      </DataTemplate>


...


    </Window.Resources>

    <DockPanel DataContext="{Binding Source={StaticResource MyList}}">
      <StackPanel>
        <Label>My Soccer Leagues</Label>
        <ListBox ItemsSource="{Binding}"
                 ItemTemplate="{StaticResource dataTemplate}"
                 IsSynchronizedWithCurrentItem="true"/>
      </StackPanel>

      <StackPanel>
        <Label Content="{Binding XPath=@name}"/>
        <ListBox Name="divisionsListBox"
                 ItemsSource="{Binding XPath=Division}"
                 ItemTemplate="{StaticResource dataTemplate}"
                 IsSynchronizedWithCurrentItem="true"/>
      </StackPanel>

      <StackPanel>
        <Label Content="{Binding XPath=@name}"/>
        <ListBox DataContext="{Binding ElementName=divisionsListBox,
                                       Path=SelectedItem}"
                 ItemsSource="{Binding XPath=Team}"
                 ItemTemplate="{StaticResource dataTemplate}"/>
      </StackPanel>
    </DockPanel>
</Window>

サンプル全体を確認するには、「XmlDataProvider を使用したマスタ詳細シナリオのサンプル」を参照してください。

参照

参照

HierarchicalDataTemplate

その他の技術情報

データ バインディングのサンプル

データ バインディングに関する「方法」トピック