1

I´m a total C# noob, so please be indulgent with me ;)

I´m just working on an WPF Application and want to implement my program in the MVVM-pattern. I want to write a simple HTML editor. So at first my application consists of a menu, which owns the item "File". There you should be able to select between "New", "Open", "Save", "Save As" and "Close". During my first steps with C# I wrote these functions in the CodeBehind-File of the MainWindow and it worked. But now I want to implement my program in a "cleaner" way by using the MVVM pattern. But I really have problems to understand this pattern and especially to implement the SaveDialog and the OpenDialog without CodeBehind.

     private void Oeffnen_Click(object sender, RoutedEventArgs e)
    {
        //neuen OpenDialog anlegen
        webBrowser2 = new WebBrowser();
        DockPanel.SetDock(webBrowser2, Dock.Top);
        this.DockPanel1.Children.Add(webBrowser2);

        opendlg = new Microsoft.Win32.OpenFileDialog();
        opendlg.Filter = "html files (*.html)|*.html|htm files (*.htm)|*.htm";
        //opendlg.RestoreDirectory = true;


        //öffne Opendialog
        if (opendlg.ShowDialog() == true)
        {
            try
            {
                if (opendlg.OpenFile() != null)
                {

                    filename = opendlg.FileName;
                    webBrowser2.Navigate("file:///" + filename);    //Öffne html Datei 

                    doc2 = webBrowser2.Document as IHTMLDocument2;
                    doc2.designMode = "On";                         //Editierbarkeit aktivieren
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
        }

So this is for example my CodeBehind for the FileOpen-function.

Can someone maybe explain me on the example of the Open or SaveDialog or anyway else, how to use the MVVM pattern with this kind of application?

Sorry if this question is too general. Please ask for details.

Thanks!

tutor_girl
  • 7
  • 1
  • 4
  • You must look at this, http://stackoverflow.com/questions/1619505/wpf-openfiledialog-with-the-mvvm-pattern – bit Apr 09 '15 at 09:19
  • Are you using a particular MVVM framework eg Prism or mvvmlight? – SWilko Apr 09 '15 at 13:46
  • you need to implement a command and bind it to a button/menu command property search for relaycommand – eran otzap Apr 09 '15 at 18:27
  • Hi, here is a complete example of a generic editor control in a WPF application utilising MVVM. The editor control can be made to work with HTML very easily and I will not describe this here... Enjoy my friend, enjoy: http://1drv.ms/1NX9JNP – MoonKnight Apr 09 '15 at 20:28
  • No I´m not using a particular MVVM framework.. should i ? – tutor_girl Apr 10 '15 at 08:02

0 Answers0