デザイン時におけるサンプルデータの表示 その2
その2は、GetIsInDesignModeメソッドを使ってデザイン時なのかどうかを判断し、デザイン時にはサンプルデータを生成するという方法です。
DesignerProperties.GetIsInDesignMode メソッド (System.ComponentModel)
この方法の場合、Expression Blend(以下Blend)でもVisual Studio 2008(以下VS2008)のWPFデザイナでも、どちらでもサンプルデータを表示することができます。
また、その1と違いサンプルデータの生成は自分で記述するわけですから、どのようなデータタイプでも問題がありませんし、出力させるデータの数も自由です。
SampleData.vb
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Public Class SampleData
Public Sub New()
MakeTestData()
End Sub
Private _mySampleData As New ObservableCollection(Of Item)
Public ReadOnly Property MySampleData() As ObservableCollection(Of Item)
Get
MySampleData = Me._mySampleData
End Get
End Property
Private ReadOnly Property IsInDesignMode()
Get
IsInDesignMode = DesignerProperties.GetIsInDesignMode(New DependencyObject())
End Get
End Property
Private Sub MakeTestData()
#If DEBUG Then
If IsInDesignMode Then
For index As Integer = 0 To 10
_mySampleData.Add(New Item("テストデータ" + index.ToString, index))
Next
#End If
Else
_mySampleData.Add(New Item("青森", 1))
_mySampleData.Add(New Item("秋田", 2))
_mySampleData.Add(New Item("岩手", 3))
_mySampleData.Add(New Item("山形", 4))
_mySampleData.Add(New Item("宮城", 5))
_mySampleData.Add(New Item("福島", 6))
End If
End Sub
End Class
Itemクラスは前回と同じですので省略しました。前回と異なるのは、バインド先をプロパティとして宣言していることと、そのプロパティにデザイン時と実行時で異なるデータを追加していることです。Imports System.ComponentModel
Public Class SampleData
Public Sub New()
MakeTestData()
End Sub
Private _mySampleData As New ObservableCollection(Of Item)
Public ReadOnly Property MySampleData() As ObservableCollection(Of Item)
Get
MySampleData = Me._mySampleData
End Get
End Property
Private ReadOnly Property IsInDesignMode()
Get
IsInDesignMode = DesignerProperties.GetIsInDesignMode(New DependencyObject())
End Get
End Property
Private Sub MakeTestData()
#If DEBUG Then
If IsInDesignMode Then
For index As Integer = 0 To 10
_mySampleData.Add(New Item("テストデータ" + index.ToString, index))
Next
#End If
Else
_mySampleData.Add(New Item("青森", 1))
_mySampleData.Add(New Item("秋田", 2))
_mySampleData.Add(New Item("岩手", 3))
_mySampleData.Add(New Item("山形", 4))
_mySampleData.Add(New Item("宮城", 5))
_mySampleData.Add(New Item("福島", 6))
End If
End Sub
End Class
Window1.xaml
<Window
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/2006"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:PreviewSampleData1="clr-namespace:PreviewSampleData2"
x:Class="Window1"
Title="Window1" Height="300" Width="300" Name="Window1">
<Window.Resources>
<ObjectDataProvider x:Key="SampleDataDS" d:IsDataSource="True" ObjectType="{x:Type PreviewSampleData1:SampleData}"/>
<DataTemplate x:Key="MySampleDataテンプレート">
<StackPanel>
<TextBlock Text="{Binding Path=MyInt}"/>
<TextBlock Text="{Binding Path=MyString}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox Margin="10"
ItemTemplate="{DynamicResource MySampleDataテンプレート}"
ItemsSource="{Binding Path=MySampleData, Mode=Default, Source={StaticResource SampleDataDS}}"
IsSynchronizedWithCurrentItem="True" Name="ListBox1" />
</Grid>
</Window>
前回と異なるのは、ListBoxのUseSampleData部分を削除したことと、バインドのパスをMySampleDataプロパティに設定したことです。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/2006"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:PreviewSampleData1="clr-namespace:PreviewSampleData2"
x:Class="Window1"
Title="Window1" Height="300" Width="300" Name="Window1">
<Window.Resources>
<ObjectDataProvider x:Key="SampleDataDS" d:IsDataSource="True" ObjectType="{x:Type PreviewSampleData1:SampleData}"/>
<DataTemplate x:Key="MySampleDataテンプレート">
<StackPanel>
<TextBlock Text="{Binding Path=MyInt}"/>
<TextBlock Text="{Binding Path=MyString}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox Margin="10"
ItemTemplate="{DynamicResource MySampleDataテンプレート}"
ItemsSource="{Binding Path=MySampleData, Mode=Default, Source={StaticResource SampleDataDS}}"
IsSynchronizedWithCurrentItem="True" Name="ListBox1" />
</Grid>
</Window>
VS2008のWPFデザイナでもサンプルデータが表示されます。