0

This seemed so simple but has turned into a nightmare for me. Everything works great, i can select a value and it's reported back to the view model.

Problem: User opens the settings flyout and selects a value. User exits the flyout. User reopens the settings flyout and there is no selected value in the combobox. The value exists in the view model though.

Scenario: Combobox in a Settingsflyout.

<ComboBox x:Name="defaultComboBox" SelectedItem="{Binding UserSettings.DefaultAccount, Mode=TwoWay}"  ItemsSource="{Binding UserAccounts}" DisplayMemberPath="CustomName">

<interactivity:Interaction.Behaviors>
            <core:EventTriggerBehavior EventName="Loaded">
                <core:InvokeCommandAction Command="{Binding UserAccountComboboxLoadedCommand}" CommandParameter="{Binding ElementName=defaultAccountComboBox}"/>
            </core:EventTriggerBehavior>
            </interactivity:Interaction.Behaviors>
</ComboBox>

ViewModelCode:

    public void Open(object parameter, Action successAction)
    {
        logger.LogProgress("Opened UserSettingsFlyoutView.");
        UserSettings.DefaultAccount =  UserAccounts.FirstOrDefault(u => u.AccountID.ToString().Equals(userSettings.DefaultAccountGuid,StringComparison.CurrentCultureIgnoreCase));
    }

    public CrossThreadObservableCollection<UserAccount> UserAccounts
    {
        get
        {
            try
            {
                return dbContext.RetrieveAllUserAccounts();
            }
            catch(Exception e)
            {
                logger.LogError("Error happened when retrieving user-accounts from secure data store Error: " + e.Message, e.ToString());
                return new CrossThreadObservableCollection<UserAccount>();
            }
        }
    } 

    private IProvideUserSetting userSettings;

    public IProvideUserSetting UserSettings
    {
        get { return userSettings; }
        set { userSettings = value; OnPropertyChanged("UserSettings"); }
    }

UserSettings class:

 private string defaultAccountGuid;
    [DataMember]
    public string DefaultAccountGuid
    {
        get { return defaultAccountGuid; }
        set { defaultAccountGuid = value; OnPropertyChanged("DefaultAccountGuid"); }
    }

    private UserAccount defaultAccount;
    [IgnoreDataMember]
    public UserAccount DefaultAccount
    {
        get { return defaultAccount; }
        set { 
            defaultAccount = value;
            if (defaultAccount != null)
                DefaultAccountGuid = defaultAccount.AccountID.ToString();
            OnPropertyChanged("DefaultAccount"); }
    }
AGCodes
  • 622
  • 1
  • 8
  • 22

1 Answers1

1

I tried a version of the code and could not reproduce the issue. Could you provide more code? Is there something else setting the selected item?

Anyway, the type of item in the ItemsSource is different than the type of item for selected item. I would try changing the selected item binding to the same class in the items source.

For example, instead of the viewmodel property UserSettings, make that object type UserAccount.

Something like

    private UserAccount _selectedUserAccount { get; set; }

    public UserAccount SelectedUserAccount
    {
        get { return _selectedUserAccount; }
        set
        {
            if (_selectedUserAccount != value)
            {
                _selectedUserAccount = value;
                OnPropertyChanged("SelectedUserAccount");
            }
        }
    }

Edit:

You can add a loaded event handler to your combobox, then locate the viewmodel from the code behind and set the selected item property.

    private void ComboBox_Loaded(object sender, RoutedEventArgs e)
    {
        ComboBox comboBox = sender as ComboBox;
        comboBox.SelectedItem =
            _viewModel.UserAccounts.Where(x => x.UserAccountString ==  _viewModel.SelectedUserAccount.UserAccountString);
    }
Bryan Stump
  • 1,419
  • 2
  • 17
  • 26
  • Just to be clear did you try this in a scenario where you will recreate the ui instance? as in you select a value and move to a different xaml page and come back to this or did you tried to select a value and the value appeared as selected in the combobox? i can definitely provide a mode simpler scenario. – AGCodes Jul 22 '14 at 02:58
  • I did not test navigation. If you're navigating can you set NavigationCacheMode="Enabled" on the page? – Bryan Stump Jul 22 '14 at 03:01
  • This is on a SettingsFlyout so there is no NavigationCacheMode. I have two toggle button on it too which persist the data changes. I guess the real problem is: The combobox will not display a selected item when it was set in the code, which might happen if the selected item instance is not the same as contained in the collection bound to the combobox, but i am making sure that i get the correct instance from the collection when i open the flyout the next time. – AGCodes Jul 22 '14 at 03:35
  • That works, don't know why SelectedItem Binding doesn't work. – AGCodes Jul 22 '14 at 05:20