WPFにあってSilverlight 2 Beta 2にないシリーズ「DataContextプロパティのコレクションサポート」
下記のようなコードでコレクションオブジェクトを作成し、
DataList.vb(Visual Basic)
Imports System.Collections.ObjectModel
Public Class DataList
Inherits ObservableCollection(Of Data)
Sub New()
For i As Integer = 1 To 10
Me.Add(New Data("テストデータ" + i.ToString()))
Next
End Sub
End Class
Public Class Data
Private _name As String
Public Property Name() As String
Get
Name = _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Sub New(ByVal _name As String)
Name = _name
End Sub
Public Overrides Function ToString() As String
Return Name
End Function
End Class
Public Class DataList
Inherits ObservableCollection(Of Data)
Sub New()
For i As Integer = 1 To 10
Me.Add(New Data("テストデータ" + i.ToString()))
Next
End Sub
End Class
Public Class Data
Private _name As String
Public Property Name() As String
Get
Name = _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Sub New(ByVal _name As String)
Name = _name
End Sub
Public Overrides Function ToString() As String
Return Name
End Function
End Class
WPFにて下記のようなXAMLを記述すると、
Window1.xaml(WPF)
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
x:Class="Window1" x:Name="Window" Title="Window1"
Width="300" Height="300" FontSize="12">
<Window.Resources>
<local:DataList x:Key="DataListDS"/>
</Window.Resources>
<StackPanel DataContext="{Binding Source={StaticResource DataListDS}}">
<TextBlock Margin="10" Text="{Binding Path=Name}" />
</StackPanel>
</Window>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
x:Class="Window1" x:Name="Window" Title="Window1"
Width="300" Height="300" FontSize="12">
<Window.Resources>
<local:DataList x:Key="DataListDS"/>
</Window.Resources>
<StackPanel DataContext="{Binding Source={StaticResource DataListDS}}">
<TextBlock Margin="10" Text="{Binding Path=Name}" />
</StackPanel>
</Window>
下記のような表示となります。
また、コレクションはCollectionViewを使ってカレントを移動することができます。
Dim myCollectionView As CollectionView
myCollectionView = CType(CollectionViewSource.GetDefaultView(LayoutRoot.DataContext), CollectionView)
myCollectionView.MoveCurrentToPrevious()
myCollectionView.MoveCurrentToNext()
myCollectionView = CType(CollectionViewSource.GetDefaultView(LayoutRoot.DataContext), CollectionView)
myCollectionView.MoveCurrentToPrevious()
myCollectionView.MoveCurrentToNext()
CollectionViewを使えば、その他にもグループ化、並べ替え、フィルタ処理などが可能です。
その他に、ListBoxのIsSynchronizedWithCurrentItemプロパティを使って、ListBoxで選択された項目とコレクションのカレントを同期させる方法もあります。
Window1.xaml(WPF)
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
x:Class="Window1" x:Name="Window" Title="Window1"
Width="300" Height="300" FontSize="12">
<Window.Resources>
<local:DataList x:Key="DataListDS"/>
</Window.Resources>
<StackPanel DataContext="{Binding Source={StaticResource DataListDS}}">
<ListBox Margin="10" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True"/>
<TextBlock Margin="10" Text="{Binding Path=Name}" />
</StackPanel>
</Window>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
x:Class="Window1" x:Name="Window" Title="Window1"
Width="300" Height="300" FontSize="12">
<Window.Resources>
<local:DataList x:Key="DataListDS"/>
</Window.Resources>
<StackPanel DataContext="{Binding Source={StaticResource DataListDS}}">
<ListBox Margin="10" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True"/>
<TextBlock Margin="10" Text="{Binding Path=Name}" />
</StackPanel>
</Window>
Silverligth 2 Beta 2の場合、下記のようなXAMLを書いても、
Page.xaml(Silverligh 2 Beta 2)
<UserControl x:Class="SilverlightApplication1.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SilverlightApplication1"
Width="300" Height="150" FontSize="24">
<UserControl.Resources>
<local:DataList x:Key="DataListDS"/>
</UserControl.Resources>
<StackPanel DataContext="{Binding Source={StaticResource DataListDS}}">
<TextBlock Margin="10" Text="{Binding Path=Name}" />
</StackPanel>
</UserControl>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SilverlightApplication1"
Width="300" Height="150" FontSize="24">
<UserControl.Resources>
<local:DataList x:Key="DataListDS"/>
</UserControl.Resources>
<StackPanel DataContext="{Binding Source={StaticResource DataListDS}}">
<TextBlock Margin="10" Text="{Binding Path=Name}" />
</StackPanel>
</UserControl>
何も表示されません。
また、当然ながらCollectionViewやIsSynchronizedWithCurrentItemプロパティはありません。