[[C#/VB/XAML] データグリッド (DataGrid) を利用するには (WPF)]をダウンロードする。
<Window x:Class="DataGrid_WPF01.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DataGrid_WPF01"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<x:Array x:Key="Books" Type="{x:Type local:Book}">
<local:Book
BookName="週刊マイクロソフト"
IsMagazine="True"
Location="新宿"
BookInfo="http://www.microsoft.com" />
<local:Book
BookName="はじめてのMS"
IsMagazine="False"
Location="大手町"
BookInfo="http://www.microsoft.com" />
<local:Book
BookName="マイクロソフト入門"
IsMagazine="False"
Location="初台"
BookInfo="http://www.microsoft.com" />
</x:Array>
</Window.Resources>
<Grid>
<DataGrid ItemsSource="{StaticResource Books}" Name="DataGrid1" Margin="0,57,0,0" />
<Button Content="Button" HorizontalAlignment="Left" Margin="35,13,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
</Grid>
</Window>
using System;
using System.Windows;
namespace DataGrid_WPF01
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var aa = ((DataGrid_WPF01.Book[])(DataGrid1.ItemsSource))[0].BookName;
}
}
public enum Branch
{新宿, 初台, 大手町, 赤坂,代田橋,調布,東京,上野 }
public class Book
{
public string BookName { get; set; }
public bool IsMagazine { get; set; }
public Branch Location { get; set; }
public Uri BookInfo { get; set; }
}
}