Click here to Skip to main content
15,906,081 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: MVVM Blues... Pin
Pete O'Hanlon26-Jan-12 10:44
mvePete O'Hanlon26-Jan-12 10:44 
GeneralRe: MVVM Blues... Pin
Tom Delany26-Jan-12 10:53
Tom Delany26-Jan-12 10:53 
GeneralRe: MVVM Blues... Pin
SledgeHammer0126-Jan-12 12:24
SledgeHammer0126-Jan-12 12:24 
GeneralRe: MVVM Blues... Pin
Tom Delany26-Jan-12 14:25
Tom Delany26-Jan-12 14:25 
GeneralRe: MVVM Blues... Pin
Dean Oliver24-Jan-12 19:05
Dean Oliver24-Jan-12 19:05 
GeneralRe: MVVM Blues... Pin
SledgeHammer0125-Jan-12 13:37
SledgeHammer0125-Jan-12 13:37 
AnswerRe: MVVM Blues... Pin
Ravi Bhavnani25-Jan-12 12:25
professionalRavi Bhavnani25-Jan-12 12:25 
GeneralRe: MVVM Blues... Pin
Kevin Marois29-Jan-12 9:23
professionalKevin Marois29-Jan-12 9:23 
I felt the same way when I first started MVVM, then I stepped back and reconsidered what the puropose of MVVM really is...To segregate your data and business layers from the UI.

Read through this little example and you'll see my point at the end.

Take for example double-clicking a list item. You want to know in the ViewModel what list item was double-clicked. You can bind the list's SelectedItem in the XAML to a property on the VM, but that handles clicking a list item, not double-clicking. To handle this, you need to handle the event. But that's not easy in MVVM.

Here's what I do to handle something like this. In the View's code behind:

First the XAML

<ListBox x:Name="MyList"
         ItemsSource="{Binding MyListItems}"
         SelectedItem="{Binding MySelectedListItem}"
         MouseDoubleClick="ListBox_MouseDoubleClick"/>


Then in the code behind:
public MainPage()
{
    InitializeComponent();
    this.DataContext = new MainPageViewModel();
}


then the event

private void ListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    ListBox list = sender as ListBox;
    ListItem item = list.SelectedItem as MyBoundType;

    var dc = this.DataContext;
    dc.ListItemDoubleClicked(item);
}


This assumes a method in your VM called ListItemDoubleClicked that accepts a parameter of the type bound to your list.

The point here is this - you can write code in your views that references your VM becuase the view already knows aout the VM. Just don't write any data-related code in the view's code behind and you're not violating VMMV.
Everything makes sense in someone's mind

AnswerRe: MVVM Blues... Pin
Dean Oliver29-Jan-12 18:50
Dean Oliver29-Jan-12 18:50 
GeneralRe: MVVM Blues... Pin
Kevin Marois30-Jan-12 7:49
professionalKevin Marois30-Jan-12 7:49 
GeneralRe: MVVM Blues... Pin
Dean Oliver30-Jan-12 18:02
Dean Oliver30-Jan-12 18:02 
GeneralRe: MVVM Blues... Pin
Kevin Marois30-Jan-12 8:03
professionalKevin Marois30-Jan-12 8:03 
GeneralRe: MVVM Blues... Pin
Dean Oliver30-Jan-12 18:03
Dean Oliver30-Jan-12 18:03 
GeneralRe: MVVM Blues... Pin
Pete O'Hanlon31-Jan-12 0:56
mvePete O'Hanlon31-Jan-12 0:56 
QuestionSilverlight App versus Silverlight Busines App Pin
Kevin Marois24-Jan-12 8:09
professionalKevin Marois24-Jan-12 8:09 
AnswerRe: Silverlight App versus Silverlight Busines App Pin
Abhinav S25-Jan-12 0:45
Abhinav S25-Jan-12 0:45 
Questiondynamicaly open elements from a xaml resource Pin
Giorgi Nistor23-Jan-12 22:37
Giorgi Nistor23-Jan-12 22:37 
AnswerRe: dynamicaly open elements from a xaml resource Pin
Giorgi Nistor23-Jan-12 23:50
Giorgi Nistor23-Jan-12 23:50 
GeneralRe: dynamicaly open elements from a xaml resource Pin
Giorgi Nistor25-Jan-12 4:07
Giorgi Nistor25-Jan-12 4:07 
QuestionMVVM architecture Pin
mi_n22-Jan-12 21:18
mi_n22-Jan-12 21:18 
AnswerRe: MVVM architecture Pin
Wayne Gaylard22-Jan-12 21:59
professionalWayne Gaylard22-Jan-12 21:59 
GeneralRe: MVVM architecture Pin
mi_n23-Jan-12 8:51
mi_n23-Jan-12 8:51 
GeneralRe: MVVM architecture Pin
Dean Oliver23-Jan-12 18:35
Dean Oliver23-Jan-12 18:35 
GeneralRe: MVVM architecture Pin
mi_n24-Jan-12 21:03
mi_n24-Jan-12 21:03 
AnswerRe: MVVM architecture Pin
Dean Oliver25-Jan-12 19:35
Dean Oliver25-Jan-12 19:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.