ソースコード
App.xaml
<Application x:Class="WpfApplication5.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> </Application>
MainWindow.xaml
<Window x:Class="WpfApplication5.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="300" Width="300"> <StackPanel> <TextBlock Text="{Binding Path=Name.FullName}" /> <TextBlock Text="{Binding Path=Sections[0].SectionName}" /> <TextBlock Text="{Binding Path=Sections[0].OfficialPosition}" /> </StackPanel> </Window>
MainWindow.xaml.cs
using System.Windows; namespace WpfApplication5 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.DataContext = new Human() { Name = new Name() { FirstName = "太郎", LastName = "山田" }, Sections = new Section[] { new Section() { SectionName = "総務部", OfficialPosition = "部長" }} }; } } }
Human.cs
namespace WpfApplication5 { class Human { public Name Name { get; set; } public Section[] Sections { get; set; } } }
Name.cs
namespace WpfApplication5 { class Name { public string FirstName { get; set; } public string LastName { get; set; } public string FullName { get { return LastName + " " + FirstName; } } } }
Section.cs
namespace WpfApplication5 { class Section { public string SectionName { get; set; } public string OfficialPosition { get; set; } } }
参考
FrameworkElement.DataContext プロパティ (System.Windows)
要素がデータ バインドに関与するときの、要素のデータ コンテキストを取得または設定します。