我們將討論的是WF4屬性窗格PropertyGrid擴展,Windows Workflow Foundation(WF),WF是一個通用的編程框架,它可用于創(chuàng)建需要對外部實體的信號作出響應(yīng)的交互式程序。
本文將講解的是WF4屬性窗格PropertyGrid擴展,希望對大家了解Windows Workflow Foundation框架有所幫助。
#T#
1. 我們有一個CaryActivity活動如下:
- namespace CaryPropertyGridExten
- {
- public sealed class CaryActivity : CodeActivity
- {
- public InArgument Text { get; set; }
- public double RepeatCount { get; set; }
- public string FileName { get; set; }
- protected override void Execute(CodeActivityContext context)
- {
- }
- }
- }
2. 上面活動有RepeatCount和FileName屬性,我們會為這兩個屬性在屬性窗格的設(shè)置自定義屬性值編輯器,要達到效果如下圖:

3. 分別定義兩個屬性對應(yīng)的屬性值編輯器如下:
- namespace CaryPropertyGridExten
- {
- class CustomInlineEditor : PropertyValueEditor
{ public CustomInlineEditor() { this.InlineEditorTemplate = new DataTemplate(); 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); FrameworkElementFactory textb = new FrameworkElementFactory(typeof(TextBox)); Binding textBinding = new Binding("Value"); textb.SetValue(TextBox.TextProperty, textBinding); textb.SetValue(TextBox.IsEnabledProperty, false); stack.AppendChild(textb); this.InlineEditorTemplate.VisualTree = stack; } } } namespace CaryPropertyGridExten { class FilePickerEditor : DialogPropertyValueEditor { public FilePickerEditor() { 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; } public override void ShowDialog(PropertyValue propertyValue, IInputElement commandSource) { Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog(); if (ofd.ShowDialog() == true) { propertyValue.Value = ofd.FileName.Substring(ofd.FileName.LastIndexOf('\\') + 1); } } } }
4. 在CaryActivity的構(gòu)造函數(shù)中增加自定義屬性的信息如下,關(guān)于AttributeTableBuilder及MetadataStore的使用可參考關(guān)于元數(shù)據(jù)存儲區(qū)MetadateStore及AttributeTableBuilder這篇文章。
- public CaryActivity()
- {
- AttributeTableBuilder builder = new AttributeTableBuilder();
builder.AddCustomAttributes(typeof(CaryActivity), "RepeatCount", new EditorAttribute(typeof(CustomInlineEditor), typeof(PropertyValueEditor))); builder.AddCustomAttributes(typeof(CaryActivity), "FileName", new EditorAttribute(typeof(FilePickerEditor), typeof(DialogPropertyValueEditor))); MetadataStore.AddAttributeTable(builder.CreateTable()); }
原文標題:WF4:屬性窗格PropertyGrid擴展
鏈接:http://www.cnblogs.com/carysun/archive/2009/11/30/WF4-PropertyGridExten.html