LINQ to SQLクラスの作成とWPFでの表示
前回の投稿のWPF編です。4番までWindowsフォーム編のときと手順が同じになるので、今回は5番から始めます。
5.Expression Blend(SP1の適用が必要)で、作成したプロジェクトを開き、[プロジェクト]パネルの下部にあるデータソースの一覧から[+CLRオブジェクト]と書いてあるボタンをクリックします。
6.[CLRオブジェクトデータソースの追加]ダイアログで「WpfApplication1」という項目を展開して「Customers」を選択し、[OK]ボタンをクリックします。
7.[資産ライブラリ]から、ListBoxコントロールをデフォルトで配置されているレイアウト要素Gridの中に配置します(大きさや位置などは適宜調整)。このとき、ListBoxの名前(Nameプロパティ)をListBox1に設定しておきます。
8.データソースの一覧にある「Customers」を配置したListBoxコントロールへドラッグ&ドロップし、表示されたメニューから「Customersを"ListBox"にバインド」をクリックします。
9.[データバインドの作成]ダイアログでは、[フィールドの選択]にItemsSourceを選択し、[OK]ボタンをクリックします。
10.[データテンプレートの作成]ダイアログは、デフォルトのまま[OK]ボタンをクリックします。
11.Expression BlendかVisual StudioのどちらかでWindowのLoadedイベントハンドラを作成し、下記のコードを記述します。
Class Window1
Private DataClasses1DataContext1 As New DataClasses1DataContext
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
ListBox1.ItemsSource = DataClasses1DataContext1.Customers()
End Sub
End Class
Private DataClasses1DataContext1 As New DataClasses1DataContext
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
ListBox1.ItemsSource = DataClasses1DataContext1.Customers()
End Sub
End Class
一応、ここまでの内容で下記のような表示となります。
なお、作成されたXAML側のObjectDataSourceは使っていません。DataTemplateを自動生成させたいがためにItemsSourceにバインドさせただけなので、ObjectDataSource部分とItemsSourceのバインド部分は削除しても問題ありません。
もう少し見た目を良くするには、ListBoxのItemsTemplateを修正します。ListBoxコントロールを右クリックしてコンテキストメニューから[他のテンプレートの編集]−[生成されたアイテムの編集(ItemsTemplate)]−[テンプレートの編集]を選択することで、ItemsTemplateの編集が可能です。
<DataTemplate x:Key="Customersテンプレート">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Address}" Width="200"/>
<Border Margin="10,0,10,0" Width="1" Background="Black"/>
<TextBlock Text="{Binding Path=City}" Width="80"/>
<Border Margin="10,0,10,0" Width="1" Background="Black"/>
<TextBlock Text="{Binding Path=CompanyName}" Width="120"/>
<Border Margin="10,0,10,0" Width="1" Background="Black"/>
<TextBlock Text="{Binding Path=ContactName}" Width="80"/>
<Border Margin="10,0,10,0" Width="1" Background="Black"/>
<TextBlock Text="{Binding Path=ContactTitle}" Width="60"/>
<Border Margin="10,0,10,0" Width="1" Background="Black"/>
<TextBlock Text="{Binding Path=Country}" Width="60"/>
</StackPanel>
</DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Address}" Width="200"/>
<Border Margin="10,0,10,0" Width="1" Background="Black"/>
<TextBlock Text="{Binding Path=City}" Width="80"/>
<Border Margin="10,0,10,0" Width="1" Background="Black"/>
<TextBlock Text="{Binding Path=CompanyName}" Width="120"/>
<Border Margin="10,0,10,0" Width="1" Background="Black"/>
<TextBlock Text="{Binding Path=ContactName}" Width="80"/>
<Border Margin="10,0,10,0" Width="1" Background="Black"/>
<TextBlock Text="{Binding Path=ContactTitle}" Width="60"/>
<Border Margin="10,0,10,0" Width="1" Background="Black"/>
<TextBlock Text="{Binding Path=Country}" Width="60"/>
</StackPanel>
</DataTemplate>
(項目数が多かったので少し減らしましたが)上記のようなDataTemplateを適用すると、下記のような表示になります。
このような表示であればListViewを使ってGridViewを用いたほうが良いですね。