代码之家  ›  专栏  ›  技术社区  ›  Sebastian Gray

WPF是否有本机文件对话框?

  •  46
  • Sebastian Gray  · 技术社区  · 16 年前

    在下面 System.Windows.Controls ,我可以看到一个 PrintDialog 然而,我似乎找不到本地人 FileDialog System.Windows.Forms 还是有别的办法?

    4 回复  |  直到 12 年前
        1
  •  63
  •   Noldorin    16 年前

    WPF有内置的(尽管没有 )文件对话框。具体来说,他们的处境有些出乎意料 Microsoft.Win32 名称空间(尽管仍然是WPF的一部分)。见 OpenFileDialog SaveFileDialog 特别是课堂。

    但是请注意,正如父命名空间所建议的那样,这些类只是Win32功能的包装。不过,这确实意味着您不需要执行任何WinForms或Win32互操作,这使它的使用更加方便。不幸的是,对话框在默认情况下是“旧”Windows主题的样式,您需要稍微修改一下 app.manifest 强迫它使用新的。

        2
  •  15
  •   Gregor Slavec    13 年前

    “打开文件”对话框可以这样使用

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <TextBox i:OpenFileDialogEx.Filter="Excel documents (.xls)|*.xls" Grid.Column="0" />
        <Button Grid.Column="1">Browse</Button>
    </Grid>
    

    OpenFileDialogEx的代码:

    public class OpenFileDialogEx
    {
        public static readonly DependencyProperty FilterProperty =
          DependencyProperty.RegisterAttached("Filter",
            typeof (string),
            typeof (OpenFileDialogEx),
            new PropertyMetadata("All documents (.*)|*.*", (d, e) => AttachFileDialog((TextBox) d, e)));
    
        public static string GetFilter(UIElement element)
        {
          return (string)element.GetValue(FilterProperty);
        }
    
        public static void SetFilter(UIElement element, string value)
        {
          element.SetValue(FilterProperty, value);
        }
    
        private static void AttachFileDialog(TextBox textBox, DependencyPropertyChangedEventArgs args)
        {                  
          var parent = (Panel) textBox.Parent;
    
          parent.Loaded += delegate {
    
            var button = (Button) parent.Children.Cast<object>().FirstOrDefault(x => x is Button);
    
            var filter = (string) args.NewValue;
    
            button.Click += (s, e) => {
              var dlg = new OpenFileDialog();
              dlg.Filter = filter;
    
              var result = dlg.ShowDialog();
    
              if (result == true)
              {
                textBox.Text = dlg.FileName;
              }
    
            };
          };
        }
    }
    
        3
  •  3
  •   Brad    12 年前

    我使用了Gregor S.提供的解决方案,效果很好,虽然我必须将其转换为VB.NET解决方案,但如果它对任何人都有帮助,下面是我的转换。。。

    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
    
        4
  •  2
  •   Daniel Scott    11 年前

    感谢Gregor S提供了一个简洁的解决方案。

    public class OpenFileDialogEx
    {
        public static readonly DependencyProperty FilterProperty =
            DependencyProperty.RegisterAttached(
                "Filter",
                typeof(string),
                typeof(OpenFileDialogEx),
                new PropertyMetadata("All documents (.*)|*.*", (d, e) => AttachFileDialog((TextBox)d, e))
            );
    
    
        public static string GetFilter(UIElement element)
        {
            return (string)element.GetValue(FilterProperty);
        }
    
        public static void SetFilter(UIElement element, string value)
        {
            element.SetValue(FilterProperty, value);
        }
    
        private static void AttachFileDialog(TextBox textBox, DependencyPropertyChangedEventArgs args)
        {
            var textBoxParent = textBox.Parent as Panel;
            if (textBoxParent == null)
            {
                Debug.Print("Failed to attach File Dialog Launching Button Click Handler to Textbox parent panel!");
                return;
            }
    
    
            textBoxParent.Loaded += delegate
            {
                var button = textBoxParent.Children.Cast<object>().FirstOrDefault(x => x is Button) as Button;
                if (button == null)
                    return;
    
                var filter = (string)args.NewValue;
    
                button.Click += (s, e) =>
                {
                    var dlg = new OpenFileDialog { Filter = filter };
    
                    var result = dlg.ShowDialog();
    
                    if (result == true)
                    {
                        textBox.Text = dlg.FileName;
                    }
                };
            };
        }
    }
    
    推荐文章