您可以自定義在設計工具中選取指定活動時所顯示的屬性方格,以建立豐富的編輯體驗。 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屬性。 也請務必包含 EditModeSwitchButton ,以提供在 FilePickerEditor.cs 中引發對話框的按鈕。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 ,然後開啟屬性方格,其中有滑桿控件和檔案選擇控制件。