.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Confused about MVVM

Posted By:      Posted Date: September 30, 2010    Points: 0   Category :WPF

I am porting an old WinForms application to WPF and using the MVVM pattern. I have not written any code using this pattern previously. So I am confused and have a fairly basic questsion

In the Old app, I had a MainWindow and many tabs. The tabs all used objects that were defined in the MainWindow. So the functionality of each tab could be applied to these "global" objects and all was good. One tab provided functionality that did a transform on an object. Another provided analysis on the current state of the same object and so on.

Now, with MVVM, I am using a similar UI model - MainWindow with many tabs where each tab has it's own viewModel and view. (don't want to change the UI on the users). My question is - where do I put the "global" objects? My model contains the classes definition for these objects. But where is the instantiation done? in which class?

Am I to create a global model? one that represents the app at runtime?  If so, how does each view/viewModel know to load the right instance of the "global" object so that it can then wrap appropriate parts in ObservableCollections etc?

Am I making sense or should I just get some shut eye tonight and RTFM tomorrow




View Complete Post

More Related Resource Links

WPF: If Heineken did MVVM Frameworks Part 4

MVVM is to have an instance of a particular Model (say currentPerson) inside my ViewModel (say PeopleViewModel) which is exposed to the View (say PeopleView). The View binds and edits the Model directly.

This definitely flies in the face of what most people consider to be the holy grail of MVVM pattern, but it's a fairly new pattern, so people are still finding their way with it every day, and this works for me very well. The reason I do what I do, is for the following reasons:

1.I have always had the luxury of being able to write my own UI specific Model classes. I would even do this if I was using some other Model classes first, such as LINQ to SQL or LINQ to Entity Framework. As these classes don't have everything a proper WPF Model class needs in my humble opinion. Though, they are pretty good, as they are Partial classes and use INotifyPropertyChanged/DataContract etc etc
2.I am a pragmatist and I do not like writing code for the sake of writing code. I have seen some MVVM apps where the author has had a Model with 50 properties on it, that are simply repeated in the ViewModel abstraction, where the ViewModel added nothing. On that day I decided I would never do that unless I have to.
3.I honestly see no harm in writing directly to the Model from the View, JUST SO LONG AS if the Model is InValid its data NEVER makes its way to the database. I honestly see no pro

Can I use MVVM for it?


I've read many articles about MVVM, but I can't get it into real code.

I have two lists which have common field user_id. First list contains all users with their names and second all files from all users. The asp.net page should show grid with newest file for every user. How can I get it into MVVM? Where should I bind username to his file? Need I ViewModel for every list or one common for both?


Regards, Gabriel

Formview + EditItemTemplate + Bind + PageLoad + Prerender = Confused


I am working with a formview edititemtemplate.  Currently in my page load, i have some stuff that would show or hide certain panels based on dropdownlist values.  These dropdown values get binded during page load.  I need to be able to change the values/dropdowns, without rebinding the old data, and updating the visibility of the panels based on predefined rules.

I have experimented with putting my formview.databind() in page load if isnotpostback, in prerender, in prerender if isnotpostback.   I have tried my rules (ex if ddlState.selectedvalue = "DE" then pnlDelaware.visible = "true") in the page load, and the page render.   I'm just not sure how to get the stars aligned here.

The closest i have come is having the databind in the pageload if notispostback, and the rules in the prerender.   However I get an error "dropdownlist has a SelectedValue which is invalid because it does not exist in the list of items.


MVVM Demo App. Josh Smith

I've been studying this code for a while now, taking notes and digging in.  I am an experienced programmer and have been around a while.  Was wondering if anyone else feels that same way I do about this demo..  It's a pretty difficult thing to get your "head around".  Does it seem that the simple thing we're trying to accomplish with the MVVM pattern is really difficult?  Or am I just a newbie to the pattern and eventually it will sink into the thich skull? I don't criticize the work done because as far as I can tell it's a nice piece of work.  All of the Abstract classes are well defined, the concrete classes are well defined, the commanding patterns are very interesting.  It just seems to me that we are jumping through hoops to do this stuff. Comments?Javaman

MVVM with navigation pages

Hi how can i do for create a WPF navigation pages application using MVVM pattern? Thanks

Confused about Disk Alignment/ Partition Offset

I am all confused by disk alignment, partiton offset.Why do we need to calcualte offset which most of the articles talk about, This is from White Paper  Disk Partition Alignment Best Practices for SQL Server where its stressed that Partition_Offset ÷ Stripe_Unit_Size-- should be an integer 'The performance question here is usually not one of correlation per the formula, but whether the cluster size is the NTFS default of 4,096 bytes or has been explicitly defined at 64 KB, which is a best practice for SQL Server.' Also how do we calculate Stripe Unit Size for SAN. Thanks all for your replies !!  

Global Values in MVVM

When developing WinForms app, I created in the Program.cs module a singleton instance of a class that held values that I could reference anywhere in the application.  The values included connection string (the user could log onto anyone of a number of databases), various options from the database including information about the user, etc. Any recommendations about how to do this within the scope of the MVVM pattern for WPF?  

MVVM and Animation Control

I've been struggling with what should be a fairly straightforward issue...hoping someone's got a solution.I'm using MVVM in a scenario where I'm using a MediaElement in a View. The MediaElement has a Storyboard that I customize using DataBinding. The scenario works well when I'm using EventTriggers to control playback, but I have not discovered how to manipulate things using DataTriggers.My Storyboard consists of a ParallelTimeline, which contains the MediaTimeline (with Source, BeginTime and Duration databound to the ViewModel. I've got two buttons that are attached to the Storyboard in EventTriggers calling PauseStoryboard and ResumeStoryboard. This works well, but it's not the best paradigm for me.I am trying to accomplish a design where, if I have two such Views each containing a MediaElement with this type of Storyboard, when one Storyboard reaches its end point, the other View's MediaElement is triggered to play.I have tried to use a DataTrigger design, but I am having problems freezing the Storyboard because I'm using DataBinding to set some of the Storyboard parameters.Does anyone have any ideas as to how I can control a MediaElement via Storyboard/Timeline using DataTrigger instead of EventTrigger? Am I missing something obvious in MVVM?--- Jim ---

WPF MVVM pattern Projects

Hello,   I'm planning to develop WPF,C# using MVVM pattern.I'm looking for Sample projects using MVVM style i.e, like we have on Asp.net Web MVC NerdDinner,MVC MusicStore,Microsoft TownHall project.Can i find similar or any other help ful links for MVVM projects?    

MVVM. Access TabControl->TabItems from ViewModel

Hi Folks,   Well, I just start to use MVVM, and I want to migrate functionality from my old non mvvm project to my new mvvm project.   The problem is the following one: In my old app (wizard application), I use tab control. And I have such things as States there. For this purpose I use StateMachine approach: My tab control looks like:   private void sm_StateChanged(State current, State previous, State next) { switch (current.Name) { case StatesNames.WELCOME_SCREEN_STATE_NAME: MainTabControl.SelectedIndex = 0; break; case StatesNames.SERVER_SUMMARY_SCREEN_STATE_NAME: MainTabControl.SelectedIndex = 1; preCertificationTabControl.SelectedIndex = 0; break; case StatesNames.SERVER_DEFINITION_SCREEN_STATE_NAME: MainTabControl.SelectedIndex = 1; preCertificationTabControl.SelectedIndex = 1; break; case StatesNames.UPGRADE_PRECERTIFICATION_SCREEN_STATE_NAME: MainTabControl.SelectedIndex = 1; preCertificationTabControl.SelectedIndex = 2; break; case StatesNames.PRECERTIFICATION_SCREEN_STATE_NAME: NavigationStateMachine.GetInstance().SwitchToNextState(); break; } } Well, as you can see, I need to access my tabControl to indicate states :( but it is not correct from

WPF MVVM RoutedEvent

Hi, I want to make a custom search control. I have followed this example about the RoutedEvent and everythings works fine. The only problem is i have now a code behind file and i whant this in my ViewModel. How can i do this? Control class public class SearchTextBox: TextBox { public static readonly RoutedEvent searchEvent = EventManager.RegisterRoutedEvent( "Search", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SearchTextBox)); public event RoutedEventHandler Search { add { AddHandler(searchEvent, value); } remove { RemoveHandler(searchEvent, value); } } void RaiseSearchEvent() { RoutedEventArgs newEventArgs = new RoutedEventArgs(SearchTextBox.searchEvent); RaiseEvent(newEventArgs); } protected override void OnTextChanged(System.Windows.Controls.TextChangedEventArgs e) { RaiseSearchEvent(); } } Application View <lib:SearchTextBox Search="....."/> Application Model ..... I've searched the internet and the related topics from MSDN but could not find the answer. Kind regards, Flaminio

Event Binding in MVVM

We are implementing the MVVM in the WPF forms that we are designing. For this we have designed the specific ViewModels for each WPF view. The WPF form has various controls such as button, combo box etc. We are registering the button click event and other events in the ViewModel constructor. Is there better way of handling this? Pls.guide

WPF startup problem using Josh Smith's MVVM example project

  Hi folks I am teaching myself the MVVM pattern in WPF, with the help of Josh Smith's legendary article found here: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx I decided to try the sample code, and then build my own WPF application from scratch, incrementally adding features based on the sample so I understand how it all fits together. I am having a few problems just getting a simple window to show and I was hoping somebody could indicate what I am doing wrong. I created a basic WPF project and used the code from the sample's Application.xaml and Application.xaml.vb, so I could use the special method for project startup that the sample illustrates. The code for Application.xaml is as follows:   <Application x:Class="JoshTest.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Application.Resources> </Application.Resources> </Application>   The Application.xaml.vb class is as follows:   Imports Microsoft.VisualBasic Imports System.Globalization Imports System.Windows Imports System.Windows.Markup Imports JoshTest.ViewModel Imports System Namespace JoshTest Partial Public Class App Inherits Application Private viewModel As MainWindowViewModel Private window As MainWindow Shared Sub

Drag and Drop Events binded to MVVM commands

Hi , I am developing the drag n drop functionality.. I want in my drop event my command from MVVM model should fire.. Can you help me in how to Map Drag drop events  into MVVM by Commands...

Using MVVM with new Ribbon Control Library

Hi, As new ribbon for wpf http://www.microsoft.com/downloads/details.aspx?FamilyID=2BFC3187-74AA-4154-A670-76EF8BC2A0B4&displaylang=en is officially released, I am looking for a way to use new ribbon control library in my application respecting the MVVM pattern, I used ribbon 2007 repecting MVVM by extending the RibbonCommand with CanExecute and Exceute properties, but Microsoft has dumped RibbonCommand in new release. Any help on new Ribbon with MVVM is appreciated.

WPF GUI fail to update -MVVM

Hi, I'm new to WPF and using MVVM in my application. The GUI has a data grid bound to a observablecollection. Then I run a second thread to update a field on the collection object. The drid won't get updated until I click on the cell. Can somebody tell me what I'm missing ? Thanks v much. Vijay

WPF, MVVM, Commands from buttons within a Listbox ItemTemplate? (code included)

I want to capture button clicks from within a list item row. Below I have tried to boil the problem down as much as I can. First, here is the xaml <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="test2.MainWindow" x:Name="Window" Title="MainWindow">  <Grid x:Name="LayoutRoot">  <ListView Margin="148,74,0,202" HorizontalAlignment="Left" ItemsSource="{Binding ListData}">   <ListView.View>    <GridView>     <GridViewColumn Header="Item" DisplayMemberBinding="{Binding ItemName}" />     <GridViewColumn Header="Options" Width="200" >      <GridViewColumn.CellTemplate>       <DataTemplate>        <StackPanel Orientation="Horizontal">         <Button Content="Reject" Height="20" Width="80" Margin="5,2" Command="{Binding Commands}" CommandParameter="RejectItem" /> 
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend