Q059. XamDataGrid に DataTable をバインドするには?

A. DataSource プロパティに DataView をバインドします。現在アクティブなデータは ActiveDataItem プロパティで取得・設定できます。以下、ViewModel を使った簡単なサンプルです。Model がないのでいわゆる MVVM パターンではありませんが、バインディングの参考程度にはなるかと思います。


Option Explicit On
Option Strict On

Imports System.ComponentModel
Imports System.Data

Public Class MainViewModel
	Implements INotifyPropertyChanged

	Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) mplements INotifyPropertyChanged.PropertyChanged

	Public Sub New()
		Me.SetData()
	End Sub

	Private _address As String
	Public Property Address() As String
		Get
			Return _address
		End Get
		Set(ByVal value As String)
			_address = value
			Me.OnPropertyChanged("Address")
		End Set
	End Property

	Private _code As String
	Public Property Code() As String
		Get
			Return _code
		End Get
		Set(ByVal value As String)
			_code = value
			Me.OnPropertyChanged("Code")
		End Set
	End Property

	Private _dataItem As DataRowView
	Public Property DataItem() As DataRowView
		Get
			Return _dataItem
		End Get
		Set(ByVal value As DataRowView)
			_dataItem = value
			Me.OnPropertyChanged("DataItem")

			If (_dataItem IsNot Nothing) Then
				Me.Code = _dataItem("code").ToString()
				Me.Name = _dataItem("name").ToString()
				Me.Address = _dataItem("address").ToString()
			End If
		End Set
	End Property

	Private _name As String
	Public Property Name() As String
		Get
			Return _name
		End Get
		Set(ByVal value As String)
			_name = value
			Me.OnPropertyChanged("Name")
		End Set
	End Property

	Private _source As DataView
	Public Property Source() As DataView
		Get
			Return _source
		End Get
		Set(ByVal value As DataView)
			_source = value
			Me.OnPropertyChanged("Source")
		End Set
	End Property

	Public Sub OnPropertyChanged(propertyName As String)
		RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
	End Sub

	Private Sub SetData()

		Dim table As New DataTable()
		table.Columns.Add("code", GetType(String))
		table.Columns.Add("name", GetType(String))
		table.Columns.Add("address", GetType(String))
		table.Columns.Add("person", GetType(String))
		table.Columns.Add("tel", GetType(String))

		table.Rows.Add(New Object() {"A00001", "田中商事", "東京都港区", "田中", "03-XXXX-XXXX"})
		table.Rows.Add(New Object() {"A00002", "山田建設", "東京都中央区", "山田", "03-XXXX-XXXX"})
		table.Rows.Add(New Object() {"A00003", "佐藤商会", "東京都新宿区", "佐藤", "03-XXXX-XXXX"})
		table.Rows.Add(New Object() {"A00004", "鈴木豆腐店", "東京都大田区", "鈴木", "03-XXXX-XXXX"})

		Me.Source = table.DefaultView
		Me.DataItem = table.DefaultView(0)

	End Sub

End Class
<Window x:Class="MainWindow"
		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
		xmlns:igDP="http://infragistics.com/DataPresenter"
		xmlns:local="clr-namespace:WpfApplication1"
		Title="MainWindow" Height="350" Width="525" >
	<Window.DataContext>
		<local:MainViewModel />
	</Window.DataContext>
	<Grid>
		<Grid.RowDefinitions>
			<RowDefinition Height="50" />
			<RowDefinition />
		</Grid.RowDefinitions>
		<igDP:XamDataGrid Grid.Row="1" DataSource="{Binding Source}" ActiveDataItem="{Binding DataItem}" />
		<Label HorizontalAlignment="Left" Margin="20,12,0,0"  Content="{Binding Code}" />
		<Label HorizontalAlignment="Left" Margin="90,12,0,0"  Content="{Binding Name}" />
		<Label HorizontalAlignment="Left" Margin="160,12,0,0" Content="{Binding Address}" />
	</Grid>
</Window>


これで10日連続 XamDataGrid ネタです。高機能で遊べる(?)コントロールなだけに、本格的に開発に使い始めて色々弄り出すとネタに困りませんねw


WPF FAQ の目次に戻る