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

  • Home
  • SEARCH
  • 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 7704699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:44:22+00:00 2026-05-31T23:44:22+00:00

I have a view with a ComboBox like this <ComboBox Name=cmbPurpose DisplayMemberPath=@value SelectedValuePath=@key ItemsSource={Binding

  • 0

I have a view with a ComboBox like this

<ComboBox Name="cmbPurpose" DisplayMemberPath="@value" SelectedValuePath="@key"
         ItemsSource="{Binding Source={StaticResource Purposes}, XPath=purpose}"
         SelectedItem="{Binding Path=Purpose, Mode=TwoWay}" />

I also set the DataContext of the view to my viewmodel. On my viewmodel i have a property ‘Purpose’ of type ‘XmlElement’ bound to SelectedItem. I would have liked it to be a string that corresponded to the @key in my xml, but i can live with this since it makes sence in a view-viewmodel context.

When i change the ComboBox, i can observe that the value in my viewmodel changes accordingly – which is as expected (and good).

When i populate my view with the viewmodel, i need some defaults set (also it will be used to edit a model later so i will need to be able to set all properties on my viewmodel). How do i set the correct value on my viewmodel’s ‘Purpose’ property? The data from my model is really equal to the @key of my xml, but the viewmodel or whatever populates my viewmodel have no way of knowing what resource to look in since it needs to set an XmlElement.

So what is best practise here? Should i move the static resource to my viewmodel or is there another way of solving this? How is this normally done? Is static resources just bad in this situation? It would be perfect if i could just make the viewmodel.Purpose into a string and have it bind to the @key instead of the whole object – that should solve the problem as i could both set and read viewmodel.Purpose without having to think of the source being xml, right?

EDIT: If there was a way to do SelectedKey="{Binding Path=Purpose, Mode=TwoWay}" and changing the viewmodel.Purpose to a string, then the problem would be sovled.

  • 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-31T23:44:23+00:00Added an answer on May 31, 2026 at 11:44 pm

    You can set the selected value to the key…. simply use SelectedValue instead of SelectedItem, and bind it to a string value which corresponds with the an @key in your ItemsSource

    <ComboBox Name="cmbPurpose" DisplayMemberPath="@value" SelectedValuePath="@key"
             ItemsSource="{Binding Source={StaticResource Purposes}, XPath=purpose}"
             SelectedValue="{Binding Path=PurposeKey, Mode=TwoWay}" />
    

    It should be noted that ComboBoxes compare items by reference, so if you do use SelectedItem then the item that is bound needs to contain the exact same reference in memory as the item in the ItemsSource.

    For example,

    SelectedItem = new SomeItem(Key = 1); // Won't work
    SelectedItem = SomeStaticList.FirstOrDefault(p => p.Key == 1); // Will work
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data grid that looks like this <tk:DataGrid ItemsSource={Binding Parents} AutoGenerateColumns=False> <tk:DataGrid.Columns>
how populate ComboBox and DataGridView using MVP (Model-View-Presenter). Actually i have something like this:
I have a Collection View Source (CVS) implemented much like you see in MSDN
In my View I have a slider and a combobox . When I change
I have a combobox in a view that receives information about application state changes,
i have view like 'home/details/5', it can be access by anonymous user. but there
I have a view user control that can post form. This control can be
I am trying to bind a combobox in WPF like this, <ComboBox Width=350 Margin=5
I have a combobox in a view that is tied to a data store
I have a simple class that provides state codes like this: public class StateProvider

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.