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.