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

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

How to raise RoutedEvent in ViewModel?

Posted By:      Posted Date: August 29, 2010    Points: 0   Category :WPF
In my UserControl.xaml - Update Button: binding to VM UpdateCommand      <Button Width="Auto" Height="24" Margin= "5" HorizontalAlignment="Right"             Command="{Binding Path=UpdateCommand}" /> in UserControl.xaml.cs - RountedEvent: UpdateClickEvent         public static readonly RoutedEvent UpdateClickEvent =             EventManager.RegisterRoutedEvent(            "UpdateClick", RoutingStrategy.Bubble,             typeof(RoutedEventHandler),             typeof(UserControl));         public event RoutedEventHandler UpdateClick        {            add { AddHandler(UpdateClickEvent, value); }            remove { RemoveHandler(Updat

View Complete Post

More Related Resource Links

Dialogs and ViewModel - Using Tasks as a Pattern

The ViewModel/MVVM pattern continues to gain popularity, with a blog post showing up every so often, and with tweets and retweets popping up even more often :-). At the same time, there are some interesting topics beyond the core pattern that continue to fuel experimentation. A big one amongst those is how should applications use dialogs when using the view model pattern.

The crux of the problem is the desire to keep the view model independent of UI concerns, and ensure it can be tested in a standalone manner, but that often comes to odds when you want the view model to launch a dialog, and/or do some work after the dialog is closed.

A Generic Dialog ViewModel

I worked on a wpf viewmodel for dialogs, because it seemed strange to me that you needed for instance windows forms dialogs to open or save files in wpf. So I set about creating my own wpf dialogs. After creating the dialog viewmodels there still remained a small issue with the library and also I didn't have much time to write an article about it. I however published the library containing the viewmodels for dialogs with another article on my blog

Design Patterns: Problems and Solutions with Model-View-ViewModel


The Model-View-ViewModel (MVVM) design pattern describes a popular approach for building Windows Presentation Foundation or Microsoft Silverlight applications. Robert McCarter shows you how the ViewModel works, and discuss some benefits and issues involved in implementing a ViewModel in your code.

Robert McCarter

MSDN Magazine July 2010

Silverlight Patterns: Model-View-ViewModel In Silverlight 2 Apps


In this article, you'll learn how to avoid problematic tight coupling by applying the Model-View-ViewModel pattern in Silverlight 2.

Shawn Wildermuth

MSDN Magazine March 2009

Patterns: WPF Apps With The Model-View-ViewModel Design Pattern


In this article we explain just how simple it can be to build a WPF application the right way using the MVVM Pattern.

Josh Smith

MSDN Magazine February 2009

Get A Raise: Discover a Series of Fortunate Event Handlers in Visual Basic


You may use them every day, but how much do you really know about events? What's going on when you add an event handler to a class? In this article, Ken Getz demonstrates various ways to interact with events and event handlers, and shows how they can solve common problems. Along with the article, you can download two sample applications (one for Visual Basic .NET 2002 and 2003 and one for Visual Basic 2005) and follow along.

Ken Getz

MSDN Magazine June 2005

MVC3: Define Dynamic ViewModel property inside Action Filter



How can I define a Dynamic ViewModel property in MVC 3 inside an action filter?

I am looking to have something like but inside the ActionFilter and not in the controller:

ViewModel.Title = "Home";

Thank You,


Viewmodel property not set to [Required] yet getting error message saying it is?


I have a viewmodel that i use for my view

public int SelectionId{ get; set; }        
public IEnumerable<Selections> Selections { get; set; }

Notice that SelectionId is not decorated with [Required]. In my view, i use DropDownFor like this:

<%= Html.DropDownListFor(m => m.SelectionId, new SelectList(Model.Selections, "Id", "Name"), "--- Select ---")%>

It should be possible to not select anything from the dropdown, resulting in model.SelectionId = 0, which is fine. Why do i get a "SelectionId field is required" error when i Post?  If i do this:

<%= Html.DropDownList(mySelectionId, new SelectList(Model.Selections, "Id", "Name"), "--- Select ---")%>

I no longer get the error if i dont select anything, which is what i want - but no way to bind it to the id if the user does select something

thanks for any help!

Custom mapping from formValues to ViewModel

Let's say i have simple ViewModel : class SimpleViewModel { public string prop1 {get;set;} public string prop2 {get;set;} } And I have following markup:<input type="text" id="prop1input" /><input type="text" id="prop2input" />I am wondering is it possible to make custom mapping from form values submitted via POST to model values, something like this:class SimpleViewModel { [HttpMappingAttr("prop1input")] public string prop1 {get;set;} [HttpMappingAttr("prop2input")] public string prop2 {get;set;} }

The model of type could not be updated viewModel

Hello,  Ok i am pritty sure this is because I dont know how to handle sending back my viewmodel on edit:Error I am dealing with:here is my [HttpGet] public ActionResult Edit(int id) { ItemEditViewModel ItemEditVM = new ItemEditViewModel(); ItemEditVM.product = storedb.Products.Single(a => a.Product_Key == id); TempData["Error"] = ""; return View(ItemEditVM); } Form is nothing specials standard strongly typed using the view model [HttpPost] public ActionResult Edit(int id, FormCollection collection) { var product = storedb.Products.Single(a => a.Product_Key == id); try { UpdateModel(product, "product"); <--- Errors Here storedb.SaveChanges();//myview model i am usingpublic class ItemEditViewModel    {        public Product product {get; set;}    } Thanks  

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

Binding WPF grid to ViewModel : Suggest the design

I have structure like this I have following structure BaseViewModel ViewModel1: BaseviewModel ViewModel2: BaseViewModel In grid i will be showing the data from both the ViewModel1 and ViewModel2 (some common data is there ) How should i go about this Expected functionality : IF i select any row in the grid i should get respective ViewModel    

Updating ViewModel values on HttpPost

Hi there, I am just getting started with MVC after many years of WebForms development. I am struggling with some basics that I hope you can help out with. I have a very simple page with two textbox fields, one called Input where a user will enter something, and one called Output where I want to return some string after they post the form, much like a postback scenario in WebForms. I have the View connected to a ViewModel for strong typing. The ViewModel: public class TestViewModel { public string Input { get; set; } public string Output { get; set; } } The View: <% Html.BeginForm(); %> <%: Html.TextBoxFor(x => x.Input) %> <input type="submit" value="Submit" /> <br /><br /> <%: Html.TextAreaFor(x => x.Output)%> <% Html.EndForm(); %>   The Controller: public ActionResult Test() { var viewModel = new TestViewModel(); viewModel.Input = "Enter something"; return View("Test",viewModel); } [HttpPost] public ActionResult Test(TestViewModel viewModel) { viewModel.Output = "Here is some response"; return View("Test", viewModel); } The problem I have is that the

MVC2 NerdDinner - Data no longer saves after adding ViewModel

I am writing this post to document some issues I ran into, and hopefully help anybody else who gets stuck in the same place.  I am creating the NerdDinner application via the Professional ASP.NET MVC 2 book,  and once I got to the ViewModel part (pages 88-91), I noticed none of my data was saving any longer.  I discovered several errors and areas of confusion in these pages of the book, which I will list now. Page 88:  When it says to add the DinnerFormViewModel class, it doesn't say what folder to add it to.  On page 89, the Inherits example (NerdDinner.Controllers.DinnerFormViewModel) implies it was added to the Controllers folder, but if that were so, the code on page 88 would need to use Models.Dinner instead of just Dinner.  No big deal; just put it in Models and change the Inherit statement on page 89 to use NerdDinner.Models.DinnerFormViewModel. Page 89:  At the bottom of the page is some wrong code.  Since the properties of Dinner are now one level deeper, all references to m.Title, m.Country, etc, need to be m.Dinner.Title, m.Dinner.Country, etc.  The statements above the code are also incorrect ("Notice... we are not changing the names of the input elements... the form elements will still be named...") -- the form elements will actually change from "Title", "Country&quo

Is it possible to pass more than one ViewModel to the View?

In my page there is a Login form. It looks like <form action="" method="post"> Login: <%:Html.TextBoxFor(m=>m.Login) %><br /> Password: <%:Html.PasswordFor(m=>m.Password) %><br /> <input type=submit value="Log In" /> <%: Html.ActionLink("Forgot my password","forgot","account") %> </form> But at the same time in this page I need to render some info from another ViewModel. Is it possible to write in action something like return View(Model1, Model2); ?And how could I get access to these ViewModels in this case?

Raise OnPropertyChanged event when Text Box Text has changed

Hi there I have a Wrapped Text Box that is bound to a Public String Property. I have a related OK Button that should be Enabled when the text changes to indicate there are 'items' in the Text Box. The problem is that the set accessor of the property (and thus OnPropertyChanged) is not being called until focus is lost from the text box - and I would like the Ok Button to be enabled before then. What is the the best way to raise the set when the Text Box text has changed? (A List Box is not an option) Thanks very much Joe
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