Задача 3. Создание области элементов и сетки свойств
В этой задаче вы создадите панели элементов и PropertyGrid и добавите их в повторно размещенное Designer рабочего процесса Windows.
Для справки код, который должен находиться в файле MainWindow.xaml.cs после выполнения трех задач в серии разделов Повторное размещение рабочего процесса Designer, приведен в конце этого раздела.
Создание области элементов и ее добавление к сетке.
Откройте проект HostingApplication, полученный с помощью процедуры, описанной в разделе Задача 2. Размещение Designer рабочего процесса.
В области Обозреватель решений щелкните правой кнопкой мыши файл MainWindow.xaml и выберите Просмотреть код.
GetToolboxControl
Добавьте метод вMainWindow
класс, который создает ToolboxControl, добавляет новую категорию панели элементов в панель элементов и назначает Assign типы действий и Sequence этой категории.private ToolboxControl GetToolboxControl() { // Create the ToolBoxControl. var ctrl = new ToolboxControl(); // Create a category. var category = new ToolboxCategory("category1"); // Create Toolbox items. var tool1 = new ToolboxItemWrapper("System.Activities.Statements.Assign", typeof(Assign).Assembly.FullName, null, "Assign"); var tool2 = new ToolboxItemWrapper("System.Activities.Statements.Sequence", typeof(Sequence).Assembly.FullName, null, "Sequence"); // Add the Toolbox items to the category. category.Add(tool1); category.Add(tool2); // Add the category to the ToolBox control. ctrl.Categories.Add(category); return ctrl; }
Добавьте частный
AddToolbox
метод вMainWindow
класс , который помещает панель элементов в левый столбец сетки.private void AddToolBox() { ToolboxControl tc = GetToolboxControl(); Grid.SetColumn(tc, 0); grid1.Children.Add(tc); }
Добавьте вызов
AddToolBox
метода в конструктореMainWindow()
класса, как показано в следующем коде:public MainWindow() { InitializeComponent(); this.RegisterMetadata(); this.AddDesigner(); this.AddToolBox(); }
Нажмите клавишу F5 , чтобы выполнить сборку и запуск решения. Должна отображаться панель элементов , Assign содержащая действия и Sequence .
Создание области PropertyGrid
В области Обозреватель решений щелкните правой кнопкой мыши файл MainWindow.xaml и выберите Просмотреть код.
AddPropertyInspector
Добавьте метод в класс ,MainWindow
чтобы разместить панель PropertyGrid в крайнем правом столбце сетки:private void AddPropertyInspector() { Grid.SetColumn(wd.PropertyInspectorView, 2); grid1.Children.Add(wd.PropertyInspectorView); }
Добавьте вызов
AddPropertyInspector
метода в конструктореMainWindow()
класса, как показано в следующем коде:public MainWindow() { InitializeComponent(); this.RegisterMetadata(); this.AddDesigner(); this.AddToolBox(); this.AddPropertyInspector(); }
Нажмите клавишу F5 , чтобы выполнить сборку и запуск решения. Панели элементов, холст конструктора рабочего процесса и PropertyGrid должны отображаться, а при перетаскивании Assign действия или Sequence действия на холст конструктора сетка свойств должна обновляться в зависимости от выделенного действия.
Пример
Файл MainWindow.xaml.cs теперь должен содержать следующий код:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
// dlls added.
using System.Activities;
using System.Activities.Core.Presentation;
using System.Activities.Presentation;
using System.Activities.Presentation.Metadata;
using System.Activities.Presentation.Toolbox;
using System.Activities.Statements;
using System.ComponentModel;
namespace HostingApplication
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private WorkflowDesigner wd;
public MainWindow()
{
InitializeComponent();
RegisterMetadata();
AddDesigner();
this.AddToolBox();
this.AddPropertyInspector();
}
private void AddDesigner()
{
// Create an instance of WorkflowDesigner class.
this.wd = new WorkflowDesigner();
// Place the designer canvas in the middle column of the grid.
Grid.SetColumn(this.wd.View, 1);
// Load a new Sequence as default.
this.wd.Load(new Sequence());
// Add the designer canvas to the grid.
grid1.Children.Add(this.wd.View);
}
private void RegisterMetadata()
{
var dm = new DesignerMetadata();
dm.Register();
}
private ToolboxControl GetToolboxControl()
{
// Create the ToolBoxControl.
var ctrl = new ToolboxControl();
// Create a category.
var category = new ToolboxCategory("category1");
// Create Toolbox items.
var tool1 =
new ToolboxItemWrapper("System.Activities.Statements.Assign",
typeof(Assign).Assembly.FullName, null, "Assign");
var tool2 = new ToolboxItemWrapper("System.Activities.Statements.Sequence",
typeof(Sequence).Assembly.FullName, null, "Sequence");
// Add the Toolbox items to the category.
category.Add(tool1);
category.Add(tool2);
// Add the category to the ToolBox control.
ctrl.Categories.Add(category);
return ctrl;
}
private void AddToolBox()
{
ToolboxControl tc = GetToolboxControl();
Grid.SetColumn(tc, 0);
grid1.Children.Add(tc);
}
private void AddPropertyInspector()
{
Grid.SetColumn(wd.PropertyInspectorView, 2);
grid1.Children.Add(wd.PropertyInspectorView);
}
}
}