0

当我右键单击 aUserControl并选择Properties时,我想在UserControl从 a 加载文件的属性中创建一个自定义属性OpenFileDialog,例如ColumnDefinitions,但它在我的机器内部浏览:

在此处输入图像描述

我怎样才能做到这一点?我一直在寻找,但我有点迷失从哪里开始。

注意:图像表明我要创建的属性是UserControl右键单击->属性时出现的属性之一UserControl

谢谢!

4

2 回答 2

1

我在 WinForm 项目的 OpenFileDialog 中声明了一个用于搜索可执行文件的属性。代码在 VB .NET 中。

首先创建一个这样的类:

Imports System.Drawing.Design
Imports System.Windows.Forms.Design
Imports System.Windows.Forms

Public Class ExecutableEditor : Inherits UITypeEditor

    Public Overrides Function GetEditStyle(context As System.ComponentModel.ITypeDescriptorContext) As System.Drawing.Design.UITypeEditorEditStyle
        Return UITypeEditorEditStyle.Modal
    End Function

    Public Overrides Function EditValue(context As System.ComponentModel.ITypeDescriptorContext, provider As System.IServiceProvider, value As Object) As Object
        If context Is Nothing And context.Instance Is Nothing And provider Is Nothing Then
            Return value
        End If

        Dim editor_service As IWindowsFormsEditorService = _
            CType(provider.GetService(GetType(IWindowsFormsEditorService)),  _
                IWindowsFormsEditorService)

        If editor_service Is Nothing Then Return value

        Dim ofdEjecutable As New OpenFileDialog
        ofdEjecutable.Title = "Select an executable file"
        ofdEjecutable.FileName = Nothing
        ofdEjecutable.Filter = "executable file|*.exe"

        If ofdEjecutable.ShowDialog = DialogResult.OK Then
            Return ofdEjecutable.FileName
        Else
            Return Nothing
        End If
    End Function

End Class

然后在 UserControl 的代码中声明一个属性,如下所示:

Private _executable As String
<Category("Injection")> _
<EditorAttribute(GetType(ExecutableEditor), GetType(UITypeEditor))> _
Public Property Executable As String
    Get
        Return _executable
    End Get
    Set(value As String)
        _executable = value
    End Set
End Property
于 2020-12-18T03:40:45.757 回答
0

我从您的问题中得到的是,您想要一个可浏览的属性供您进行用户控制。通过这个,对于简单的 .net 属性添加:

    private string myString;
    [Browsable(true)]
    [Category("Other")]
    public string MyProperty { get { return myString; } set { myString = value; } }

并在属性的设置器中加载验证后的文件。

如果您希望它是依赖属性,请执行相同的操作,但将加载文件的代码移动到 propertychange 处理程序中。

于 2013-08-27T09:32:28.113 回答