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 reference to System.Windows.Forms
or is there another way?
In this article Dialog boxes are used to: Display specific information to users. Gather information from users. Both display and gather information. Display an operating system prompt, such as print window.
Create a WPF project using Visual Studio and add a TextBox, a Button, and a TextBlock control to page. The final Window looks like Figure 2. Figure 2. When you click the Browse button, we will browse text files and set the selected file name as the text of the TextBox.
Windows Presentation Foundation (WPF) supports browser-style navigation that can be used in two types of applications: standalone applications and XAML browser applications (XBAPs). To package content for navigation, WPF provides the Page class.
WPF does have built-in (although not native) file dialogs. Specifically, they are in the slightly unexpected Microsoft.Win32
namespace (although still part of WPF). See the OpenFileDialog
and SaveFileDialog
classes in particular.
Do however note that these classes are only wrappers around the Win32 functionality, as the parent namespace suggests. It does however mean that you don't need to do any WinForms or Win32 interop, which makes it somewhat nicer to use. Unfortunately, the dialogs are by default style in the "old" Windows theme, and you need a small hack in app.manifest
to force it to use the new one.
You can create a simple attached property to add this functionality to a TextBox. Open file dialog can be used like this:
<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>
The code for 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; } }; }; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With