Does WPF have a native file dialog?

前端 未结 4 633
迷失自我
迷失自我 2020-12-23 16:09

Under System.Windows.Controls, I can see a PrintDialog However, I can\'t seem to find a native FileDialog. Do I need to create a refe

4条回答
  •  被撕碎了的回忆
    2020-12-23 16:50

    I used the solution presented by Gregor S. and it works well, although I had to convert it to a VB.NET solution, here is my conversion if it helps anyone...

    Imports System
    Imports Microsoft.Win32
    
    Public Class OpenFileDialogEx
        Public Shared ReadOnly FilterProperty As DependencyProperty = DependencyProperty.RegisterAttached("Filter", GetType(String), GetType(OpenFileDialogEx), New PropertyMetadata("All documents (.*)|*.*", Sub(d, e) AttachFileDialog(DirectCast(d, TextBox), e)))
        Public Shared Function GetFilter(element As UIElement) As String
            Return DirectCast(element.GetValue(FilterProperty), String)
        End Function
    
        Public Shared Sub SetFilter(element As UIElement, value As String)
            element.SetValue(FilterProperty, value)
        End Sub
    
    
        Private Shared Sub AttachFileDialog(textBox As TextBox, args As DependencyPropertyChangedEventArgs)
            Dim parent = DirectCast(textBox.Parent, Panel)
            AddHandler parent.Loaded, Sub()
    
              Dim button = DirectCast(parent.Children.Cast(Of Object)().FirstOrDefault(Function(x) TypeOf x Is Button), Button)
              Dim filter = DirectCast(args.NewValue, String)
                AddHandler button.Click, Sub(s, e)
                   Dim dlg = New OpenFileDialog()
                   dlg.Filter = filter
                   Dim result = dlg.ShowDialog()
                   If result = True Then
                       textBox.Text = dlg.FileName
                   End If
                End Sub
            End Sub
        End Sub
    End Class
    

提交回复
热议问题