Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何为UserControl创建自定义OpenFiledialog属性_Wpf_Properties_Wpf Controls_Dependency Properties - Fatal编程技术网

Wpf 如何为UserControl创建自定义OpenFiledialog属性

Wpf 如何为UserControl创建自定义OpenFiledialog属性,wpf,properties,wpf-controls,dependency-properties,Wpf,Properties,Wpf Controls,Dependency Properties,当我右键单击UserControl并选择Properties时,我想在UserControl属性中创建一个自定义属性,该属性从OpenFileDialog加载文件,如列定义,但在我的机器内浏览: 我怎样才能做到这一点?我一直在寻找,但我有点不知道从哪里开始 注意:图像表明我要创建的属性是UserControl的属性之一,当您右键单击->properties该UserControl时会显示该属性 谢谢 我从您的问题中得到的是,您希望为您的用户控件提供一个可浏览的属性。这样,对于简单的.net属性

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

我怎样才能做到这一点?我一直在寻找,但我有点不知道从哪里开始

注意:图像表明我要创建的属性是
UserControl
的属性之一,当您右键单击->properties
UserControl
时会显示该属性


谢谢

我从您的问题中得到的是,您希望为您的用户控件提供一个可浏览的属性。这样,对于简单的.net属性添加:

    private string myString;
    [Browsable(true)]
    [Category("Other")]
    public string MyProperty { get { return myString; } set { myString = value; } }
并在属性setter中加载验证后的文件


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

我声明了一个属性,用于在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
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
然后在UserControl的代码中声明如下属性:

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
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
Private\u可执行文件作为字符串
_
_
作为字符串的公共属性可执行文件
得到
返回\u可执行文件
结束
设置(值为字符串)
_可执行文件=值
端集
端属性

是否希望在用户控件中定义依赖项属性?还是附属财产?另外,您希望何时加载文件?一旦属性值发生变化?一个附加值,并且我希望在运行项目时加载文件(因此我猜这是基本值…)。为什么不在用户控件的加载事件中打开一个文件对话框?因为用户可能不需要加载文件。是我的UserControl的一个选项。这就是为什么我需要它成为UserControl的一个属性。图片是用来做什么的?这似乎与这个问题没有关系。或多或少。我需要它在右边有一个打开OpenFileDialog的按钮。有什么想法吗?