Skip to content

Open file dialog

Mattias Kindborg edited this page May 19, 2016 · 2 revisions

To show a open file dialog start by registering the view by decorating the XAML with the attached property DialogServiceViews.IsRegistered.

<UserControl
    x:Class="DemoApplication.Features.OpenFileDialog.Views.OpenFileTabContent"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:md="https://github.com/fantasticfiasco/mvvm-dialogs"
    md:DialogServiceViews.IsRegistered="True">
  
</UserControl>

In the view model, open the dialog by calling IDialogService.ShowOpenFileDialog.

public class OpenFileTabContentViewModel : INotifyPropertyChanged
{
    private readonly IDialogService dialogService;
  
    public OpenFileTabContentViewModel(IDialogService dialogService)
    {
        this.dialogService = dialogService;
    }
  
    private void OpenFile()
    {
        var settings = new OpenFileDialogSettings
        {
            Title = "This Is The Title",
            InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
            Filter = "Text Documents (*.txt)|*.txt|All Files (*.*)|*.*"
        };

        bool? success = dialogService.ShowOpenFileDialog(this, settings);
        if (success == true)
        {
            Path = settings.FileName;
        }
    }
}