0

I want to implement OpenFileDialog In WPF application in MVVM pattern. I have added a interface model class. But don't know how its properly implemented.

Mainwindow.xaml

<Window x:Class="FileManager.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:FileManager"
        mc:Ignorable="d"
         Style="{StaticResource RedStyle}"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
    <TextBlock Text="ExFile Manager Application" HorizontalAlignment="Center" Margin="20,5,20,0" />
<TextBox HorizontalAlignment="Center" Height="43" Margin="30,35,0,0" TextWrapping="Wrap"   
                 Text="Select only the text file" VerticalAlignment="Top" Width="476" Name="FileNameTextBox"/>  
        <Button x:Name="BrowseButton"  Content="Browse a file" HorizontalAlignment="Right"  Click="BrowseButton_Click"  
                Margin="485,35,10,0" VerticalAlignment="Top" Width="121"  
                RenderTransformOrigin="1.047,0.821" Height="40"/>  
        <TextBlock HorizontalAlignment="Left" Height="282" Margin="30,96,0,0"   
                   TextWrapping="Wrap" VerticalAlignment="Top"  
                   Width="703" Name="TextBlock1"/>  
    </Grid>
</Window>

Mainwindow.cs

namespace FileManager
{
   
    public partial class MainWindow : Window
    {
        private IOpenFileService _IOpenFileService;
        public MainWindow()
        {
            InitializeComponent();
        }
        public string FileNames{
        get { return _IOpenFileService.FileName;}
        set {_IOpenFileService.FileName=value; }
        
        }
        public string result{
            get{return _IOpenFileService.result;}
            set{_IOpenFileService.result=value;}
        }
        private void BrowseButton_Click(object sender, RoutedEventArgs e)
        {
            // Create OpenFileDialog

            Microsoft.Win32.OpenFileDialog openFileDlg = new Microsoft.Win32.OpenFileDialog();
            // Set filter for file extension and default file extension  
            openFileDlg.DefaultExt = ".txt";
            openFileDlg.Filter = "Text documents (.txt)|*.txt";

            // Launch OpenFileDialog by calling ShowDialog method
            //specifies whether the activity was accepted (true) or canceled (false).
            //ShowDialog() is called on a window that is closing (Closing) or has been closed (Closed).
            Nullable<bool> result = openFileDlg.ShowDialog();
            // Get the selected file name and display in a TextBox.
            // Load content of file in a TextBlock
            if (result == true)
            {


                FileNameTextBox.Text = openFileDlg.FileName;
                TextBlock1.Text = System.IO.File.ReadAllText(openFileDlg.FileName);
            }
            
        }
    }
}

Model.cs

namespace FileManagerModel.Model
{
    public interface IOpenFileService
    {
        string FileName { get; set;}
         string result  {get; set;}
        
    }
}

I have attached the XAML file and XAML.cs files along with this

I am new in dotnet core and WPf application. Also please check my coding styles and structure. I also want to implement this in MVVM pattern.

  • *"please check my coding styles and structure"* - [codereview](https://codereview.stackexchange.com/). – Sinatr Sep 25 '20 at 12:08
  • Does this answer your question? [Open File Dialog MVVM](https://stackoverflow.com/questions/1043918/open-file-dialog-mvvm) – Sinatr Sep 25 '20 at 12:08
  • will you please implement it on my code? –  Sep 25 '20 at 12:20
  • It doesn't loos like MVVM, you need to: 1) create ViewModel 2) move code from view into VM 3) Use bindings/commands. Then see duplicate, you just need concrete implementation of dialog interface which you pass into VM, so you can call `ShowDialog()` from command. – Sinatr Sep 25 '20 at 12:29
  • Your current implementation is 100% MVVM (almost). Showing the file dialog should be done from the click handler. Don't use a service to delegate this to the view model. Especially when the dialog is triggered by the view (user). This is nonsense and will overcomplicate a trivial problem. Code-behind does not violate MVVM. MVVM is about separation of business logic from view logic. Showing a dialog is view logic as a dialog is a simple control/interface to collect user input -> UI. – BionicCode Sep 25 '20 at 16:13
  • Once the dialog is closed you can send the data to the view model using data binding or commands. Then in your view model (or model) you can handle the input and do the I/O. I/O is not part of the view. Data handling should be inside your model. – BionicCode Sep 25 '20 at 16:14

0 Answers0