首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Workflow 4.5中的UITypeEditor (浏览文件夹)

Workflow 4.5中的UITypeEditor (浏览文件夹)
EN

Stack Overflow用户
提问于 2016-09-19 10:50:12
回答 1查看 407关注 0票数 0

我试图在WF4.5Workflow活动中实现Browsefor文件夹,但是省略号按钮没有显示,几乎什么也没有发生。

这是我的UITypeEditor课程:

代码语言:javascript
复制
public class BrowseForFolderEditor : UITypeEditor
{
    public override object EditValue(ITypeDescriptorContext context, 
         IServiceProvider provider, object value)
    {
        string folderName = string.Empty;
        BrowseForFolderAttribute browseForFolderAttribute = null;

        if (value is string)
        {
            if (context?.PropertyDescriptor != null)
            {
                browseForFolderAttribute =
                    (BrowseForFolderAttribute)
                context.PropertyDescriptor.Attributes[typeof(BrowseForFolderAttribute)];
            }

            var browse = new FolderBrowserDialogEx
            {
                Description = browseForFolderAttribute?.Description,
                ShowNewFolderButton = true,
                ShowEditBox = true,
                SelectedPath = folderName,
                ShowFullPathInEditBox = false,
                RootFolder = Environment.SpecialFolder.MyComputer
            };

            var result = browse.ShowDialog();

            if (result == DialogResult.OK)
                folderName = browse.SelectedPath;

            return folderName;
        }

        // Return whatever value if it wasn't a string - Should never occur!
        return value;
    }

    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal; //base.GetEditStyle(context);
    }

    public class BrowseForFolderAttribute : Attribute
    {
        public BrowseForFolderAttribute(string description)
        {
            this.Description = description;
        }

        public string Description { get; set; }
    }
}

我就是这样在我的Activity中声明代码的

代码语言:javascript
复制
    [Description("Select the folder where the files will be 
     copied/moved to.")]
    [Category("Folders")]
    [Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    [BrowseForFolderEditor.BrowseForFolder("Select the folder where the files will be 
     copied/moved to.")]
    [Editor(typeof(BrowseForFolderEditor), typeof(UITypeEditor))]
    public string TargetPath { get; set; }

我不知道这是否有什么区别,但是我的工作流ActivityNativeActivity类型的。

属性显示在属性网格中,但它只是显示为没有省略按钮的文本框。

任何帮助都将不胜感激。

更新-1:

这个问题与它是一个NativeCodeActivity这一事实没有任何关系,因为我刚刚将我的代码更改为CodeActivity,这并没有什么区别。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-19 23:15:56

我通过查看Microsoft提供的一些示例(即用于.NET框架4的Windows通信基础(WCF)和Workflow (WF)示例 )就知道了这一点。

无论如何,基于这个信息,在文本框太短而无法显示路径的情况下,我设法显示了省略号按钮(‘.’)、文本框和工具提示。它还不完美,因为我正在研究如何添加额外的属性来设置“BrowseForFolder”对话框的其他属性,但是我想我应该分享我的发现。

我的编辑器定义如下:

代码语言:javascript
复制
public class BrowseForFolderEditor : DialogPropertyValueEditor
{
    public BrowseForFolderEditor()
    {
        // Create a textbox, a '...' button and a tooltip.
        string template = @"
            <DataTemplate
                xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
                xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
                xmlns:pe='clr-namespace:System.Activities.Presentation.PropertyEditing;assembly=System.Activities.Presentation'>
                <DockPanel LastChildFill='True'>
                    <pe:EditModeSwitchButton TargetEditMode='Dialog' Name='EditButton' DockPanel.Dock='Right'>...</pe:EditModeSwitchButton>
                    <TextBlock Text='{Binding Value}' Margin='2,0,0,0' VerticalAlignment='Center'>
                        <TextBox.ToolTip>
                            <ToolTip>
                                <TextBlock Text='{Binding Value}' Margin='2' VerticalAlignment='Center' HorizontalAlignment='Left'/>
                            </ToolTip>
                        </TextBox.ToolTip>
                    </TextBlock>
                </DockPanel>
            </DataTemplate>";

        using (var sr = new MemoryStream(Encoding.UTF8.GetBytes(template)))
        {
            this.InlineEditorTemplate = XamlReader.Load(sr) as DataTemplate;
        }
    }

    public override void ShowDialog(PropertyValue propertyValue, 
      IInputElement commandSource)
    {
        var browse = new FolderBrowserDialog
        {
            ShowNewFolderButton = true,
            SelectedPath = propertyValue.StringValue,
            Description = "Select Target Folder:",
            RootFolder = Environment.SpecialFolder.MyComputer
        };

        if (browse.ShowDialog() == DialogResult.OK)
        {
            propertyValue.Value = browse.SelectedPath;
        }

        browse.Dispose();
    }
}

关于XAML,我不会说太多的细节,但是有几件事需要注意:

  1. 如何设置InlineEditorTemplate,即将其设置为字符串中预定义的XAML。
  2. TextBox的绑定即值
  3. 工具提示的绑定,即值

Activity构造函数中包含的“重要”代码是:

代码语言:javascript
复制
AttributeTableBuilder builder = new AttributeTableBuilder();

builder.AddCustomAttributes(typeof(CopyMoveFiles), "TargetPath",
            new EditorAttribute(
            typeof(BrowseForFolderEditor), 
            typeof(DialogPropertyValueEditor)));

MetadataStore.AddAttributeTable(builder.CreateTable());

其中,TargetPath表示将显示在PropertyGrid中的字符串属性。

当然还有改进的余地,但这是一个开始,而且似乎效果很好。值得一提的是,添加各种引用是一件痛苦的事情。我不能再在这个问题上浪费时间了,但是即使按照微软的项目添加了所有的引用之后,它仍然不能工作,最终还是起了作用,所以我仍然不知道为什么会发生这种情况--这是一个遗憾,但是如果有人尝试并且能够找出哪些引用给他们带来了麻烦,我很想知道。

希望这能有所帮助。

更新:

如果不希望以编程方式在构造函数中定义属性,只需使用以下属性:

代码语言:javascript
复制
[Editor(typeof(BrowseForFolderEditor), typeof(DialogPropertyValueEditor))]
public string TargetPath { get; set; }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39571338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档