WF4:属性窗格PropertyGrid扩展

简介:

1.       我们有一个CaryActivity活动如下:

namespace CaryPropertyGridExten

{

    public sealed class CaryActivity : CodeActivity

    {       

        public InArgument<string> Text { getset; }

        public double RepeatCount { getset; }

        public string FileName { getset; }          

        protected override void Execute(CodeActivityContext context)

        {                      

        }

    }

}

2.       上面活动有RepeatCountFileName属性,我们会为这两个属性在属性窗格的设置自定义属性值编辑器要达到效果如下图:

clip_image002

3.       分别定义两个属性对应的属性值编辑器如下:

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的构造函数中增加自定义属性的信息如下,关于AttributeTableBuilderMetadataStore的使用可参考关于元数据存储区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());

        }  

 

例子来源:WF_WCF_Samples


本文转自生鱼片博客园博客,原文链接:http://www.cnblogs.com/carysun/archive/2009/11/30/WF4-PropertyGridExten.html,如需转载请自行联系原作者

相关文章
WPF中如何获取选中行/单元格所在行的数据
WPF中如何获取选中行/单元格所在行的数据
WPF TreeView设置所有节点默认展开
WPF TreeView设置所有节点默认展开
|
C#
WPF整理-为控件添加自定义附加属性
原文:WPF整理-为控件添加自定义附加属性 附加属性,大家都不陌生,最常见的是Canvas.Left/Canvas.Top,类似的也有Grid.Row/Grid.Column等附加属性。举个最常见的例子 需要说明的是并不是所有的附加属性都是元素放进去后才会有附加效果,上面的例子只是刚好是这种错觉的巧合情况,Grid.Row也属于这种巧合。
2110 0
|
Web App开发
艾伟:WinForm控件开发总结(七)-----为复杂属性的子属性提供编辑功能
前面的几篇文章中,我们给控件添加一个复杂的类型Scope,并且给它的类型提供的一个类型转换器,现在我们可以在属性浏览器中编辑它的值,并且它的值也被串行化的源代码里了。但是你有没有发现,在属性浏览器里编辑这个属性的值还是不太方便。
691 0
|
C#
WPF 创建自定义窗体
原文:WPF 创建自定义窗体 在前面的一篇博客"WPF 自定义Metro Style窗体",展示了如何创建一个类似于Metro Style的Window,并在程序中使用。但是这个窗体不能够自由的改变大小。
1392 0
|
C# 前端开发
WPF Adorner+附加属性 实现控件友好提示
原文:WPF Adorner+附加属性 实现控件友好提示 标题太空泛,直接上图   无论是在验证啊,还是提示方面等一些右上角的角标之类的效果,我们会怎么做? 这里介绍一种稍微简单一些的方法,利用附加属性和Adorner来完成。
986 0
|
前端开发 C#
WPF 介绍一种在MVVM模式下弹出子窗体的方式
原文:WPF 介绍一种在MVVM模式下弹出子窗体的方式 主要是通过一个WindowManager管理类,在window后台代码中通过WindowManager注册需要弹出的窗体类型,在ViewModel通过WindowManager的Show方法,显示出来。
2316 0
|
C#
WPF自定义行为Behavior,实现双击控件复制文本
原文:WPF自定义行为Behavior,实现双击控件复制文本 WPF引用xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.
1079 0
|
C#
WPF中实现PropertyGrid(用于展示对象的详细信息)的三种方式
原文:WPF中实现PropertyGrid(用于展示对象的详细信息)的三种方式 由于WPF中没有提供PropertyGrid控件,有些业务需要此类的控件。
1953 0
|
C# 数据安全/隐私保护
WPF 使用附加属性增加控件属性
原文:WPF 使用附加属性增加控件属性 使用附加属性增加控件属性,使得这个附加属性在使用的时候没有局限性,可以在任何的控件中使用它来增加所需要的属性,使得控件的属性使用起来非常灵活   一、自定义附加属性 1 2 3 4 5 6 7 8 9 10 11 ...
888 0