Note that there are some explanatory texts on larger screens.

plurals
  1. POSilverlight Combobox - Setting the SelectedItem MVVM
    text
    copied!<p>I have a ViewModel that sets the value for the "UserStructure" property. The problem is that the combobox wont bind to the value.</p> <pre><code>public class OwnerOccupierAccountViewModel : ViewModelBase { /// &lt;summary&gt; /// Load combobox structures /// &lt;/summary&gt; private readonly LoadOperation&lt;Structure&gt; _loadStructures; private readonly LoadOperation&lt;UnitOccupierDetail&gt; _loadUnitOccupierDetails; //public ICommand SaveAccountSettingsCommand { get; set; } #region Properties private ObservableCollection&lt;Structure&gt; _structures; public ObservableCollection&lt;Structure&gt; Structures { get { return _structures; } set { _structures = value; RaisePropertyChanged("Structures"); } } private Structure _userStructure; public Structure UserStructure { get { return _userStructure; } set { _userStructure = value; RaisePropertyChanged("SelectedStructure"); } } private UnitOccupierDetail _unitOccupierDetail; public UnitOccupierDetail UnitOccupierDetail { get { return _unitOccupierDetail; } set { _unitOccupierDetail = value; RaisePropertyChanged("UnitOccupierDetail"); } } #endregion public OwnerOccupierAccountViewModel() { // SaveAccountSettingsCommand = new DelegateCommand(SaveAccountSettings, CanSave); UserAccountContext _userAccountContext; if (!DesignerProperties.IsInDesignTool) { var loggedInUser = new Guid(WebContext.Current.User.UserID.ToString()); _userAccountContext = new UserAccountContext(); #region load structures _loadStructures = _userAccountContext.Load(_userAccountContext.GetStructuresQuery()); _loadStructures.Completed += _loadStructuresCompleted; #endregion #region load user data _loadUnitOccupierDetails = _userAccountContext.Load( _userAccountContext.GetUnitOccupierDetailsQuery().Where( u =&gt; u.UserIDFK == loggedInUser &amp;&amp; u.StructureFK == 92)); _loadUnitOccupierDetails.Completed += _loadUnitOccupierDetails_Completed; #endregion } } void _loadUnitOccupierDetails_Completed(object sender, EventArgs e) { _unitOccupierDetail= new UnitOccupierDetail(); _unitOccupierDetail = _loadUnitOccupierDetails.Entities.First(); _userStructure = _unitOccupierDetail.Structure; } void _loadStructuresCompleted(object sender, EventArgs e) { var theseStructures = new ObservableCollection&lt;Structure&gt;(_loadStructures.Entities); Structures = theseStructures; } //private void SaveAccountSettings(object param) //{ //} //private static bool CanSave(object param) //{ // return true; //} } &lt;ComboBox x:Name="cboApartments" ItemsSource='{Binding Structures, Mode=TwoWay}' DisplayMemberPath='StructureName' SelectedValuePath='IDStructure' SelectedItem='{Binding SelectedStructure,Mode=TwoWay}' Width="200" Height="25"&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload