1. 我们有一个CaryActivity活动如下:
namespace CaryPropertyGridExten
{
public sealed class CaryActivity : CodeActivity
{
public InArgument<string> Text { get; set; }
public double RepeatCount { get; set; }
public string FileName { get; set; }
protected override void Execute(CodeActivityContext context)
{
}
}
}
2. 上面活动有RepeatCount和FileName属性,我们会为这两个属性在属性窗格的设置自定义属性值编辑器,要达到效果如下图:
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的构造函数中增加自定义属性的信息如下,关于AttributeTableBuilder及MetadataStore的使用可参考关于元数据存储区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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
2008-11-30 WF中的跟踪服务(5):SqlTrackingService 的数据维护