可以自定义在设计器中选择给定活动时显示的属性网格,从而创建丰富的编辑体验。 PropertyGridExtensibility 示例演示如何执行此作。
演示
工作流设计器属性网格的扩展性。
讨论
为了扩展属性网格,开发人员可以选择自定义属性网格编辑器的嵌入式外观,或者提供一个对话框,以便用于更高级的编辑界面。 此示例中演示了两个不同的编辑器;内联编辑器和对话框编辑器。
内联编辑器
内联编辑器示例演示以下内容:
创建派生自 PropertyValueEditor的类型。
在构造函数中,该值 InlineEditorTemplate 是使用 Windows Presentation Foundation (WPF) 数据模板设置的。 这可以绑定到 XAML 模板,但在此示例中,代码用于初始化数据绑定。
数据模板具有在属性网格中呈现的项的 PropertyValue 的数据上下文。 请注意,以下代码(来源于 CustomInlineEditor.cs)中,该上下文随后绑定到
Value属性。FrameworkElementFactory stack = new FrameworkElementFactory(typeof(StackPanel)); FrameworkElementFactory slider = new FrameworkElementFactory(typeof(Slider)); Binding sliderBinding = new Binding("Value"); sliderBinding.Mode = BindingMode.TwoWay; slider.SetValue(Slider.MinimumProperty, 0.0); slider.SetValue(Slider.MaximumProperty, 100.0); slider.SetValue(Slider.ValueProperty, sliderBinding); stack.AppendChild(slider);由于活动和设计器位于同一程序集中,因此活动设计器属性的注册在活动本身的静态构造函数中完成,如以下示例所示,如SimpleCodeActivity.cs中的以下示例所示。
static SimpleCodeActivity() { AttributeTableBuilder builder = new AttributeTableBuilder(); builder.AddCustomAttributes(typeof(SimpleCodeActivity), "RepeatCount", new EditorAttribute(typeof(CustomInlineEditor), typeof(PropertyValueEditor))); builder.AddCustomAttributes(typeof(SimpleCodeActivity), "FileName", new EditorAttribute(typeof(FilePickerEditor), typeof(DialogPropertyValueEditor))); MetadataStore.AddAttributeTable(builder.CreateTable()); }
对话框编辑器
对话框编辑器示例演示了以下内容:
创建派生自 DialogPropertyValueEditor的类型。
在构造函数中,使用 WPF 数据模板设置 InlineEditorTemplate 值。 这可以在 XAML 中创建,但在此示例中,这是在代码中创建的。
数据模板具有在属性网格中呈现的项的 PropertyValue 的数据上下文。 在下面的代码中,稍后会将其绑定到
Value属性。 在 FilePickerEditor.cs 中,还必须包含一个 EditModeSwitchButton,以便提供弹出对话框的按钮。this.InlineEditorTemplate = new DataTemplate(); FrameworkElementFactory stack = new FrameworkElementFactory(typeof(StackPanel)); stack.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal); FrameworkElementFactory label = new FrameworkElementFactory(typeof(Label)); Binding labelBinding = new Binding("Value"); label.SetValue(Label.ContentProperty, labelBinding); label.SetValue(Label.MaxWidthProperty, 90.0); stack.AppendChild(label); FrameworkElementFactory editModeSwitch = new FrameworkElementFactory(typeof(EditModeSwitchButton)); editModeSwitch.SetValue(EditModeSwitchButton.TargetEditModeProperty, PropertyContainerEditMode.Dialog); stack.AppendChild(editModeSwitch); this.InlineEditorTemplate.VisualTree = stack;重写设计器类型中的 ShowDialog 方法以处理对话框的显示。 在此示例中,显示了基本 FileDialog。
public override void ShowDialog(PropertyValue propertyValue, IInputElement commandSource) { Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog(); if (ofd.ShowDialog() == true) { propertyValue.Value = ofd.FileName; } }由于活动和设计器位于同一程序集中,因此活动设计器属性的注册在活动本身的静态构造函数中完成,如以下示例所示,如SimpleCodeActivity.cs中的以下示例所示。
static SimpleCodeActivity() { AttributeTableBuilder builder = new AttributeTableBuilder(); builder.AddCustomAttributes(typeof(SimpleCodeActivity), "RepeatCount", new EditorAttribute(typeof(CustomInlineEditor), typeof(PropertyValueEditor))); builder.AddCustomAttributes(typeof(SimpleCodeActivity), "FileName", new EditorAttribute(typeof(FilePickerEditor), typeof(DialogPropertyValueEditor))); MetadataStore.AddAttributeTable(builder.CreateTable()); }
设置、生成和运行示例
生成解决方案,然后打开 Workflow1.xaml。
将 SimpleCodeActivity 从工具箱拖到设计器画布上。
单击 SimpleCodeActivity ,然后打开属性网格,其中有滑块控件和文件选取控件。