0

Hi i am using WPF and Devexpress! I am new to c# world and have no experience in MVVM. I have watched different videos to learn MVVM. But all are related to basics of MVVM (Why it is efficient, etc) I wrote code in WPF. There are three buttons in my code : Add new data, Edit Data and Refresh Grid. Each button has it's click event defining it's functionality. I want to convert this simple basic WPF code into MVVM framework. Can anyone guide me for it's conversion to MVVM. Edition

     void EditRow(int focRowHand, Entities a)
        {

        Name nametext = grid.GetRow(focRowHand) as Name;

        try
            {
            if (nametext.Name1 != string.Empty)
                {

                update_id = nametext.ID;
                txtName2.Text = update_text = nametext.Name;

                if (Panel3.Visibility == System.Windows.Visibility.Visible)
                    {
                    Panel1.Visibility = System.Windows.Visibility.Visible;
                    Panel3.Visibility = System.Windows.Visibility.Collapsed;
                    }

                else
                    {
                    Panel1.Visibility = System.Windows.Visibility.Collapsed;
                    Panel3.Visibility = System.Windows.Visibility.Visible;
                    }
                }
            }
        catch (Exception err)
            {
            DXMessageBox.Show(err.StackTrace);

            }

        }

        private void Button1_Copy_Click(object sender, RoutedEventArgs e)
            {

            if (view.FocusedRowHandle == 0)
                {
                DXMessageBox.Show("Please Select any Item From Grid List");
                }
            else
                {
                try
                    {
                    int FocRowHand = view.FocusedRowHandle;
                    Entities a = new Entities();

                    if (grid.IsGroupRowHandle(FocRowHand))
                        {
                        int childCount = grid.GetChildRowCount(FocRowHand);
                        for (int i = 0; i < childCount; i++)
                            {
                            int childHandle = grid.GetChildRowHandle(FocRowHand, i);
                            EditRow(childHandle, a);
                            }
                        }
                    else
                        {
                        EditRow(FocRowHand, a);
                        }

                    }
                catch (Exception ee)
                    {
                    DXMessageBox.Show(ee.StackTrace);
                    }
                }

            }

Insertion

    private void Insertion()
  {
  if (txtName.Text != string.Empty)
      {
      if (DXMessageBox.Show("Are You Sure, you Want to Insert?", "Insert Item-Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
          {
          try
              {

                  Entities dbContext = new Entities();
                  Name name = new Name();
                  name.my_name = txtName.Text;
                  dbContext.Names.Add(name);
                  dbContext.SaveChanges();
                  txtName.Text = null;
                  Refresh();
                  }

          catch (Exception err)
              {
              DXMessageBox.Show(err.StackTrace);

              }
          }
      else
          txtName.Text = null;
      }

  }

If my question is not clear to you or you want more information please ask me. Thank you :)

Zoya Sheikh
  • 871
  • 3
  • 11
  • 21
  • 2
    if you're using mvvm, where is your XAML? – default Aug 26 '13 at 12:36
  • I am not using MVVM. I want this simple code into MVVM. Please carefully read my query. Thank you @Default – Zoya Sheikh Aug 26 '13 at 12:38
  • @Default `I want to convert this code into MVVM. Can anyone guide me for it's conversion to MVVM.` – Viv Aug 26 '13 at 12:39
  • 1
    perhaps you could add what you understood from the videos you saw? this could help give a more accurate answer – ppetrov Aug 26 '13 at 12:39
  • You have rather complex code that would not be easy to just tell you one thing and answer your question. – Ryan Amies Aug 26 '13 at 12:39
  • 1
    well, that's really not how Stackoverflow works. Could you show what you have tried and why that did not work as expected? A tip, if you want to convert "Button_click" events from winforms (which I guess this is?) look at the `Command` property on a WPF Button – default Aug 26 '13 at 12:40
  • @ZoyaSheikh it could also be good, but my point here is that it's better someone explains you what you missed or didn't see in the videos rather than just converting your code, so the references can be good, but the valuable thing here would be that you explain in your terms what you understood from these videos – ppetrov Aug 26 '13 at 12:43

2 Answers2

3

I think no one will spend time writing code for you. You have to learn it by youself by learning some tutorials. Perhaps you can follow some very good tutorials which give you insight into developing MVVM based application (Not only focusing on efficiency)

Follow this thread:

MVVM: Tutorial from start to finish?

It contains all you need.

Community
  • 1
  • 1
Irfan
  • 2,713
  • 3
  • 29
  • 43
1

Start by creating your ViewModel class and creating methods that deal with each situation (add, edit, etc). Then in your event handlers just call the relevant ViewModel function. Taking a look at your code you will have to pass through some state information such as Panel3.Visibility.

Once you've done this you can begin to move your design to a totally MVVM pattern by binding relevant properties to your VM instead of passing it through.

The simplest method seems to be insertion. To make it fully MVVM you will have to bind your txtName to a property. I'd start there.

Ryan Amies
  • 4,902
  • 1
  • 21
  • 36
  • I've been taught not to use event handlers, but rather the `Command` property and `ICommand` interface. This ensures that logic is minimized in the view. – default Aug 26 '13 at 12:43
  • 1
    That's right, but its more complexity to deal with right away. As I said, I'd start with what I've suggested and then progress. First move the code to the VM, then eventually it should be easy to create commands – Ryan Amies Aug 26 '13 at 12:46