Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 720533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:46:09+00:00 2026-05-14T05:46:09+00:00

i’m just starting with the mvvm model in Silverlight. In step 1 i got

  • 0

i’m just starting with the mvvm model in Silverlight.
In step 1 i got a listbox bound to my viewmodel, but now i want to propagate a click in a button and a selecteditemchanged of the listbox back to the viewmodel.
I guess i have to bind the click event of the button and the selecteditemchanged of the listbox to 2 methods in my viewmodel somehow?

For the selecteditemchanged of the listbox i think there must also be a ‘return call’ possible when the viewmodel tries to set the selecteditem to another value?

i come from a asp.net (mvc) background, but can’t figure out how to do it in silverlight.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:46:09+00:00Added an answer on May 14, 2026 at 5:46 am

    I would not bind or tie the VM in any way directly to the events of controls within the View. Instead, have a separate event that is raised by the View in response to the button click.

    [disclaimer: this code is all done straight from my head, not copy & pasted from VS – treat it as an example!!]

    So in pseudo code, the View will look like this:

    private void MyView_Loaded(...) 
    {
        MyButton.Click += new EventHandler(MyButton_Click);
    }
    
    private void MyButton_Click(...)
    {
        //Raise my event:
        OnUserPressedGo();
    }
    
    private void OnUserPressedGo()
    {
        if (UserPressedTheGoButton != null)
            this.UserPressedTheGoButton(this, EventArgs.Empty);
    }
    
    public EventHandler UserPressedTheGoButton;
    

    and the VM would have a line like this:

    MyView.UserPressedTheGoButton += new EventHandler(myHandler);
    

    this may seem a little long-winded, why not do it a bit more directly? The main reason for this is you do not want to tie your VM too tightly (if at all) to the contents of the View, otherwise it becomes difficult to change the View. Having one UI agnostic event like this means the button can change at any time without affecting the VM – you could change it from a button to a hyperlink or that kool kat designer you hire may change it to something totally weird and funky, it doesn’t matter.

    Now, let’s talk about the SelectedItemChanged event of the listbox. Chances are you want to intercept an event for this so that you can modify the data bound to another control in the View. If this is a correct assumption, then read on – if i’m wrong then stop reading and reuse the example from above 🙂

    The odds are that you may be able to get away with not needing a handler for that event. If you bind the SelectedItem of the listbox to a property in the VM:

    <ListBox ItemSource={Binding SomeList} SelectedItem={Binding MyListSelectedItem} />
    

    and then in the MyListSelectedItem property of the VM:

    public object MyListSelectedItem 
    {
        get { return _myListSelectedItem; }
        set 
        {
            bool changed = _myListSelectedItem != value;
            if (changed) 
            {
                _myListSelectedItem = value;
                OnPropertyChanged("MyListSelectedItem");
            }
        }
    }
    
    private void OnPropertyChanged(string propertyName) 
    {
        if (this.NotifyPropertyChanged != null)
            this.NotifyPropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    

    To get that NotifyPropertyChanged event, just implement the INotifyPropertyChanged interface on your VM (which you should have done already). That is the basic stuff out of the way… what you then follow this up with is a NotifyPropertyChanged event handler on the VM itself:

    private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e) 
    {
        switch (e.PropertyName)
        {
            case "MyListSelectedItem":
                //at this point i know the value of MyListSelectedItem has changed, so 
                //i can now retrieve its value and use it to modify a secondary 
                //piece of data:
                MySecondaryList = AllAvailableItemsForSecondaryList.Select(p => p.Id == MyListSelectedItem.Id);
                break;
        }
    }
    

    All you need now is for MySecondaryList to also notify that its value has changed:

    public List<someObject> MySecondaryList 
    {
        get { return _mySecondaryList; }
        set 
        {
            bool changed = .......;
            if (changed)
            {
                ... etc ...
                OnNotifyPropertyChanged("MySecondaryList");
            }
        }
    }
    

    and anything bound to it will automatically be updated. Once again, it may seem that this is the long way to do things, but it means you have avoided having any handlers for UI events from the View, you have kept the abstraction between the View and the ViewModel.

    I hope this has made some sense to you. With my code, i try to have the ViewModel knowing absolutely zero about the View, and the View only knowing the bare minimum about the ViewModel (the View recieves the ViewModel as an interface, so it can only know what the interface has specified).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.