L2DBForm.xaml ソース コード
このページには、LINQ to XML サンプルを使用した
全体的な UI 構造
WPF プロジェクトの一般的なように、このファイルには 1 つの親要素(LinqToXmlDataBinding
名前空間の派生クラスに関連付けられている Window XML 要素 L2XDBFrom
が含まれています。
クライアント領域は、明るい青色の背景を持つ StackPanel 内に含まれています。 このパネルには、Separator バーで区切られた 4 つの DockPanel UI セクションが含まれています。 これらのセクションの目的は、ここで説明されています。
各セクションには、それを識別するラベルが含まれています。 最初の 2 つのセクションでは、このラベルは、LayoutTransformを使用して 90 度回転します。 セクションの残りの部分には、テキスト ブロック、テキスト ボックス、ボタンなど、そのセクションの目的に適した UI 要素が含まれています。 これらの子コントロールの配置には、子 StackPanel が使用されている場合があります。
ウィンドウ リソース セクション
9 行目の開始 <Window.Resources>
タグは、ウィンドウ リソース セクションの開始を示します。 35 行目の終了タグで終わります。
11 行目から 25 行目にまたがる <ObjectDataProvider>
タグは、XElement をソースとして使用する LoadedBooks
という名前の ObjectDataProviderを宣言します。 XElement は、埋め込み XML ドキュメント (CDATA
要素) を解析することによって初期化されます。 埋め込み XML ドキュメントを宣言するとき、および解析時にも空白が保持されていることに注意してください。 空白は保持されます。未加工の XML の表示に使用される TextBlock コントロールには、特別な XML 書式設定機能がないためです。
最後に、BookTemplate
という名前の DataTemplate は、28 行目から 34 行目で定義されます。 このテンプレートは、Book List UI セクションのエントリを表示するために使用されます。 データ バインディングと LINQ to XML 動的プロパティを使用して、次の割り当てを使用して書籍 ID と書籍名を取得します。
Text="{Binding Path=Attribute[id].Value}"Text="{Binding Path=Value}"
データ バインディング コード
DataTemplate 要素に加えて、データ バインディングは、このファイル内の他の多くの場所で使用されます。
38 行目の開始 <StackPanel>
タグでは、このパネルの DataContext プロパティが LoadedBooks
データ プロバイダーに設定されます。
DataContext="{Binding Source={StaticResource LoadedBooks}}
データ コンテキストを設定すると、46 行目で tbRawXml
という名前の TextBlock が、このデータ プロバイダーの Xml
プロパティにバインドして、生の XML を表示することが可能になります。
Text="{Binding Path=Xml}"
58 行目から 62 行目の Book List UI セクションの ListBox は、表示項目のテンプレートをウィンドウ リソース セクションで定義された BookTemplate
に設定します。
ItemTemplate ="{StaticResource BookTemplate}"
次に、59 行目から 62 行目で、書籍の実際の値がこのリスト ボックスにバインドされます。
<ListBox.ItemsSource>
<Binding Path="Elements[{http://www.mybooks.com}book]"/>
</ListBox.ItemsSource>
3 番目の UI セクション 選択した書籍の編集では、最初に親 StackPanel の DataContext を、Book List UI セクション (82 行目) から現在選択されている項目にバインドします。
DataContext="{Binding ElementName=lbBooks, Path=SelectedItem}"
次に、双方向のデータ バインディングを使用して、ブック要素の現在の値が、このパネルの 2 つのテキスト ボックスに対して表示および更新されるようにします。 動的プロパティへのデータ バインディングは、BookTemplate
データ テンプレートで使用されるデータ バインディングに似ています。
Text="{Binding Path=Attribute[id].Value}"...Text="{Binding Path=Value}"
最後の UI セクション 新しいブックの追加では、XAML コードでデータ バインディングは使用されません。 代わりに、データ バインディングは、L2DBForm.xaml.csファイル内のイベント処理コードにあります。
例
説明
手記
次のコードを Visual Studio の C# ソース コード エディターなどのコード エディターにコピーして、行番号を追跡しやすくすることをお勧めします。
コード
<Window x:Class="LinqToXmlDataBinding.L2XDBForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:xlinq="clr-namespace:System.Xml.Linq;assembly=System.Xml.Linq"
xmlns:local="clr-namespace:LinqToXmlDataBinding"
Title="WPF Data Binding using LINQ-to-XML" Height="665" Width="500" ResizeMode="NoResize">
<Window.Resources>
<!-- Books provider and inline data -->
<ObjectDataProvider x:Key="LoadedBooks" ObjectType="{x:Type xlinq:XElement}" MethodName="Parse">
<ObjectDataProvider.MethodParameters>
<system:String xml:space="preserve">
<![CDATA[
<books xmlns="http://www.mybooks.com">
<book id="0">book zero</book>
<book id="1">book one</book>
<book id="2">book two</book>
<book id="3">book three</book>
</books>
]]>
</system:String>
<xlinq:LoadOptions>PreserveWhitespace</xlinq:LoadOptions>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<!-- Template for use in Books List listbox. -->
<DataTemplate x:Key="BookTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Margin="3" Text="{Binding Path=Attribute[id].Value}"/>
<TextBlock Margin="3" Text="-"/>
<TextBlock Margin="3" Text="{Binding Path=Value}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<!-- Main visual content container -->
<StackPanel Background="lightblue" DataContext="{Binding Source={StaticResource LoadedBooks}}">
<!-- Raw XML display section -->
<DockPanel Margin="5">
<Label Background="Gray" FontSize="12" BorderBrush="Black" BorderThickness="1" FontWeight="Bold">XML
<Label.LayoutTransform>
<RotateTransform Angle="90"/>
</Label.LayoutTransform>
</Label>
<TextBlock Name="tbRawXml" Height="200" Background="LightGray" Text="{Binding Path=Xml}" TextTrimming="CharacterEllipsis" />
</DockPanel>
<Separator Height="4" Margin="5" />
<!-- List box to display all books section -->
<DockPanel Margin="5">
<Label Background="Gray" FontSize="12" BorderBrush="Black" BorderThickness="1" FontWeight="Bold">Book List
<Label.LayoutTransform>
<RotateTransform Angle="90"/>
</Label.LayoutTransform>
</Label>
<ListBox Name="lbBooks" Height="200" Width="415" ItemTemplate ="{StaticResource BookTemplate}">
<ListBox.ItemsSource>
<Binding Path="Elements[{http://www.mybooks.com}book]"/>
</ListBox.ItemsSource>
</ListBox>
<Button Margin="5" DockPanel.Dock="Right" Height="30" Width ="130" Content="Remove Selected Book" Click="OnRemoveBook">
<Button.LayoutTransform>
<RotateTransform Angle="90"/>
</Button.LayoutTransform>
</Button>
</DockPanel>
<Separator Height="4" Margin="5" />
<!-- Edit current selection section -->
<DockPanel Margin="5">
<TextBlock Margin="5" Height="30" Width="65" DockPanel.Dock="Right" Background="LightGray" TextWrapping="Wrap" TextAlignment="Center">
Changes are live!
<TextBlock.LayoutTransform>
<RotateTransform Angle="90"/>
</TextBlock.LayoutTransform>
</TextBlock>
<StackPanel>
<Label Width="450" Background="Gray" FontSize="12" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" FontWeight="Bold">Edit Selected Book</Label>
<StackPanel Margin="1" DataContext="{Binding ElementName=lbBooks, Path=SelectedItem}">
<StackPanel Orientation="Horizontal">
<Label Width="40">ID:</Label>
<TextBox Name="editAttributeTextBox" Width="410" Text="{Binding Path=Attribute[id].Value}">
<TextBox.ToolTip>
<TextBlock FontWeight="Bold" TextAlignment="Center">
<Label>Edit the selected book ID and see it changed.</Label>
</TextBlock>
</TextBox.ToolTip>
</TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Width="40">Value:</Label>
<TextBox Name="editValueTextBox" Width="410" Text="{Binding Path=Value}" Height="25">
<TextBox.ToolTip>
<TextBlock FontWeight="Bold" TextAlignment="Center">
<Label>Edit the selected book Value and see it changed.</Label>
</TextBlock>
</TextBox.ToolTip>
</TextBox>
</StackPanel>
</StackPanel>
</StackPanel>
</DockPanel>
<Separator Height="4" Margin="5" />
<!-- Add new book section -->
<DockPanel Margin="5">
<Button Margin="5" Height="30" DockPanel.Dock="Right" Click ="OnAddBook">Add Book
<Button.LayoutTransform>
<RotateTransform Angle="90"/>
</Button.LayoutTransform>
</Button>
<StackPanel>
<Label Width="450" Background="Gray" FontSize="12" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" FontWeight="Bold">Add New Book</Label>
<StackPanel Margin="1">
<StackPanel Orientation="Horizontal">
<Label Width="40">ID:</Label>
<TextBox Name="tbAddID" Width="410">
<TextBox.ToolTip>
<TextBlock FontWeight="Bold" TextAlignment="Center">
<Label>Enter a book ID and Value pair, then click Add Book.</Label>
</TextBlock>
</TextBox.ToolTip>
</TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Width="40">Value:</Label>
<TextBox Name="tbAddValue" Width="410" Height="25">
<TextBox.ToolTip>
<TextBlock FontWeight="UltraBold" TextAlignment="Center">
<Label>Enter a book ID and Value pair, then click Add Book.</Label>
</TextBlock>
</TextBox.ToolTip>
</TextBox>
</StackPanel>
</StackPanel>
</StackPanel>
</DockPanel>
</StackPanel>
</Window>
コメント
WPF UI 要素に関連付けられているイベント ハンドラーの C# ソース コードについては、ソース コード
関連項目
.NET Desktop feedback