<UserControl Loaded="UserControl_Loaded">
このトピックは、プロシージャ (C#、VB) コードを使用して xamOrgChart を追加する方法を説明します。最後に、完全なコードが個別のセクションに提供されます。手順を先に進める前に、 「xamOrgChart をアプリケーションに追加」トピックの要件を満たしていることを確認してください。
xamOrgChart コントロールをユーザーのアプリケーションに追加し、データ モデルにバインドします。
UserControl の Loaded イベントにイベント ハンドラーを追加します。
XAML の場合:
<UserControl Loaded="UserControl_Loaded">
コード ビハインドに using/Imports を配置します。
Visual Basic の場合:
Imports Infragistics.Controls.Maps Imports UsingXamOrgChart
C# の場合:
using Infragistics.Controls.Maps; using UsingXamOrgChart;
UserControl_Loaded イベント ハンドラー内で、DataContext を DepartmentViewModel オブジェクトにバインドし、ItemsSource を Departments プロパティにバインドして、xamOrgChart コントロールのインスタンスを作成します。xamOrgChart の新しいインスタンスを Grid パネルの Children コレクションに追加します (初めて作成する時には、デフォルトで「LayoutRoot」という名前のグリッド パネルが定義されます)。
Visual Basic の場合:
Dim data As New DepartmentViewModel() Dim OrgChart As New XamOrgChart() OrgChart.DataContext = data OrgChart.ItemsSource = data.Departments Me.LayoutRoot.Children.Add(OrgChart)
C# の場合:
DepartmentViewModel data = new DepartmentViewModel(); XamOrgChart Org Chart = new XamOrgChart(); OrgChart.DataContext = data; OrgChart.ItemsSource = data.Departments; this.LayoutRoot.Children.Add(OrgChart);
ノード レイアウトを OrgChart に追加します。
ノード レイアウト インスタンスを作成し、それらを xamOrgChart コントロールの GlobalNodeLayouts コレクションに追加します。
Visual Basic の場合:
Dim departmentNodeLayout As New OrgChartNodeLayout() departmentNodeLayout.TargetTypeName = "Department" departmentNodeLayout.DisplayMemberPath = "Name" departmentNodeLayout.Key = "EmployeePositions" Dim positionNodeLayout As New OrgChartNodeLayout() positionNodeLayout.TargetTypeName = "EmployeePosition" positionNodeLayout.DisplayMemberPath = "JobTitle" positionNodeLayout.Key = "Employees" Dim employeeNodeLayout As New OrgChartNodeLayout() employeeNodeLayout.TargetTypeName = "Employee" employeeNodeLayout.DisplayMemberPath = "FirstName" OrgChart.GlobalNodeLayouts.Add(departmentNodeLayout) OrgChart.GlobalNodeLayouts.Add(positionNodeLayout) OrgChart.GlobalNodeLayouts.Add(employeeNodeLayout)
C# の場合:
OrgChartNodeLayout departmentNodeLayout = new OrgChartNodeLayout(); departmentNodeLayout.TargetTypeName = "Department"; departmentNodeLayout.DisplayMemberPath = "Name"; departmentNodeLayout.Key = "EmployeePositions"; OrgChartNodeLayout positionNodeLayout = new OrgChartNodeLayout(); positionNodeLayout.TargetTypeName = "EmployeePosition"; positionNodeLayout.DisplayMemberPath = "JobTitle"; positionNodeLayout.Key = "Employees"; OrgChartNodeLayout employeeNodeLayout = new OrgChartNodeLayout(); employeeNodeLayout.TargetTypeName = "Employee"; employeeNodeLayout.DisplayMemberPath = "FirstName"; OrgChart.GlobalNodeLayouts.Add(departmentNodeLayout); OrgChart.GlobalNodeLayouts.Add(positionNodeLayout); OrgChart.GlobalNodeLayouts.Add(employeeNodeLayout);
Visual Basic の場合:
Private Sub UserControl_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) Dim data As New DepartmentViewModel() Dim OrgChart As New XamOrgChart() OrgChart.DataContext = data OrgChart.ItemsSource = data.Departments Me.LayoutRoot.Children.Add(OrgChart) Dim departmentNodeLayout As New OrgChartNodeLayout() departmentNodeLayout.TargetTypeName = "Department" departmentNodeLayout.DisplayMemberPath = "Name" departmentNodeLayout.Key = "EmployeePositions" Dim positionNodeLayout As New OrgChartNodeLayout() positionNodeLayout.TargetTypeName = "EmployeePosition" positionNodeLayout.DisplayMemberPath = "JobTitle" positionNodeLayout.Key = "Employees" Dim employeeNodeLayout As New OrgChartNodeLayout() employeeNodeLayout.TargetTypeName = "Employee" employeeNodeLayout.DisplayMemberPath = "FirstName" OrgChart.GlobalNodeLayouts.Add(departmentNodeLayout) OrgChart.GlobalNodeLayouts.Add(positionNodeLayout) OrgChart.GlobalNodeLayouts.Add(employeeNodeLayout) End Sub
C# の場合:
private void UserControl_Loaded(object sender, RoutedEventArgs e) { DepartmentViewModel data = new DepartmentViewModel(); XamOrgChart Org Chart = new XamOrgChart(); OrgChart.DataContext = data; OrgChart.ItemsSource = data.Departments; this.LayoutRoot.Children.Add(OrgChart); OrgChartNodeLayout departmentNodeLayout = new OrgChartNodeLayout(); departmentNodeLayout.TargetTypeName = "Department"; departmentNodeLayout.DisplayMemberPath = "Name"; departmentNodeLayout.Key = "EmployeePositions"; OrgChartNodeLayout positionNodeLayout = new OrgChartNodeLayout(); positionNodeLayout.TargetTypeName = "EmployeePosition"; positionNodeLayout.DisplayMemberPath = "JobTitle"; positionNodeLayout.Key = "Employees"; OrgChartNodeLayout employeeNodeLayout = new OrgChartNodeLayout(); employeeNodeLayout.TargetTypeName = "Employee"; employeeNodeLayout.DisplayMemberPath = "FirstName"; OrgChart.GlobalNodeLayouts.Add(departmentNodeLayout); OrgChart.GlobalNodeLayouts.Add(positionNodeLayout); OrgChart.GlobalNodeLayouts.Add(employeeNodeLayout); }