当我右键单击UserControl
并选择属性时,我想在UserControl
属性中创建一个自定义属性,用于从OpenFileDialog
加载文件,像 ColumnDefinitions ,但在我的机器内浏览:
我怎样才能实现这一目标?我一直在寻找,但我有点迷失在哪里开始。
注意:图像表示我要创建的属性是右键单击>属性 UserControl
时显示的UserControl
属性之一
谢谢!
答案 0 :(得分:0)
我从您的问题中得到的是您想要一个可浏览的属性供您用户控制。为此,对于简单的.net属性添加:
private string myString;
[Browsable(true)]
[Category("Other")]
public string MyProperty { get { return myString; } set { myString = value; } }
并且在属性的setter中验证后加载文件。
如果您希望它是依赖属性,请执行相同操作,但在propertychange处理程序中移动加载文件的代码。
答案 1 :(得分:0)
我声明了一个属性,用于在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