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 8080675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:29:17+00:00 2026-06-05T16:29:17+00:00

I’m learning MVVM. I have my View filling two comboboxes from ObservableCollection properties in

  • 0

I’m learning MVVM.

I have my View filling two comboboxes from ObservableCollection properties in my ViewModel (eg. properties “Oc1” & “Oc2“). I also have a property bound to the selected item of Oc1 (eg. property “SelVal“) that Oc2 depends on, so when property SelVal is changed, Oc2 needs to re-get it’s data from the database.

Now, I’ve come up with a solution and it works for my situation but doesn’t seem to adhere to the principle of a get accessor so I’d like to know what problem might I face down the track and what is a better solution?

My current solution is:

The get accessor of Oc2 queries the database and sets it’s private field to the value returned from the database (which the View uses). So when SetVal is changed, I simply call this.RaisePropertyChanged(“Oc2“) in the SetVal set accessor and the View asks for Oc2, which in turn queries the database and returns the updated list.
The problem is that I’m not using the get accessor for what it is meant for, as I’m assigning it’s value in it. But what I like about it is it’s self-contained (ie. I don’t need a “BindOc2” method which I’d have to call in the constructor and then again in the SelVal set accessor). Please advise. And what’s a better way?

  • 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-06-05T16:29:19+00:00Added an answer on June 5, 2026 at 4:29 pm

    Your suspicion is correct, this way breaks the MVVM model and fails to use mechanisms that can simplify your work, like the triggers in System.Windows.Interactivity which is available in the Expression Blend SDK.

    Loading the data in the getter is begging for trouble. It binds your model to the data access code, makes testing harder and the code more complex by mixing different concerns in the same property. Besides, Murphy’s law dictates that at some point you will just want to set the property without reloading from the database.

    A better solution is to extract the data loading code in a separate method that can be called by a command or a trigger. This method will either modify the Oc2 collection’s contents or simply replace it with a new collection. In any case, the property setter will raise the proper notification and the second combo will be updated.

    The following example comes from fabien’s answer in a similar SO question:

    <ComboBox x:Name="fileComboBox" ItemsSource="{Binding FileList, Mode=TwoWay}">                  
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction 
                            Command="{Binding SelectionChangedCommand}"
                            CommandParameter="{Binding SelectedItems,           
                            ElementName=fileComboBox}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
    
        </ComboBox>
    

    Most frameworks provide support for this design. Caliburn.Micro provides actions that will call a ViewModel method when a UI event occurs, and a shortcut syntax to avoid writing the trigger XAML and the corresponding command. It simply connects the event to a ViewModel method. The XAML can be as simple as this:

    <ComboBox x:Name="Oc1" 
        cal:Message.Attach="[Event SelectionChanged] = 
                            [Action ReloadFor($this.SelectedItem)]" />
    

    The $this value is another shorthand that references the ComboBox itself.

    If you don’t want to use triggers, you can bind the SelectedItem property of your combo to a ViewModel property and execute the Reload method whenever this property changes, eg.:

    <ComboBox ... SelectedItem={Binding CurrentOC2,Mode=TwoWay} />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.