2 Replies - 8307 Views - Last Post: 13 July 2011 - 07:39 AM

#1 Ahmedn1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 23
  • View blog
  • Posts: 556
  • Joined: 04-August 09

MVVM Problem

Posted 10 July 2011 - 02:16 PM

Hello DICs,
I'm developing a web application using the MVVM model
I now want to modify a datagrid control in the view through the view model code
how can I do that?
how can I make the view model class see the view controls?
Is This A Good Question/Topic? 0
  • +

Replies To: MVVM Problem

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: MVVM Problem

Posted 12 July 2011 - 03:41 AM

You break the MVVM pattern if you give UIElement references to ViewModel. Only View should know about UI elements while ViewModel handles the business logic.

One way to deliver messages from VM to V is to use events. Have a class which both VM and V knows which has the events and also methods to raise the events.

Small sample:
public class MyEvents {
  public event ModifyView;

  public void RaiseModifyView() { 
    ModifyView();
  }
}

public class View : Window {
  private MyEvents e = new MyEvents;

  public View() {
    InitializeComponents();
    this.DataContext = new ViewModel(e);
    e.ModifyView += () => 
      {
         // do something
      }
  }
}

public class ViewModel {
  public ViewModel(MyEvents e) {
    e.RaiseModifyView();
  }
}



This is all I can tell you based on the information you provided. If you need more suggestions, give us more details about your situation.

Btw. Not writing any code-behind can make your program messy. The way I use MVVM is that if there is something that is purely UI logic, I write it code behind or somewhere else but ViewModel because ViewModel only holds the business. For example if I want to change background color on button click, I'll write it code behind because UI logic doesn't need to change the color since the color has no connections to business. Of course if the color is changed based on some business object, like boolean property (true = green, false = red), I'll bind the UI element to the business and use converter.
(Bad example because you could do that in XAML I think, using event triggers and stuff. But anyway...)
Was This Post Helpful? 1
  • +
  • -

#3 Ahmedn1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 23
  • View blog
  • Posts: 556
  • Joined: 04-August 09

Re: MVVM Problem

Posted 13 July 2011 - 07:39 AM

I have another problem
I need to get an ID from a table
but when I use
 LoadOperation<table name>

I cannot deal with it as I deal with a DataTable object
how can I get a specific value from the returning data
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1