【问题标题】:Combobox in settingsflyout does not show selected values when opening it againsettingsflyout 中的组合框在再次打开时不显示选定的值
【发布时间】:2014-09-10 09:42:45
【问题描述】:

这看起来很简单,但对我来说却变成了一场噩梦。一切都很好,我可以选择一个值并将其报告回视图模型。

问题: 用户打开设置弹出窗口并选择一个值。用户退出浮出控件。 用户重新打开设置浮出控件,组合框中没有选定值。该值存在于视图模型中。

场景: 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 类:

 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"); }
    }

【问题讨论】:

    标签: combobox windows-store-apps winrt-xaml windows-8.1 flyout


    【解决方案1】:

    我尝试了一个版本的代码,但无法重现该问题。你能提供更多代码吗?是否还有其他设置所选项目?

    无论如何,ItemsSource 中的项目类型与所选项目的项目类型不同。我会尝试将所选项目绑定更改为项目源中的同一类。

    例如,代替 viewmodel 属性 UserSettings,使该对象类型为 UserAccount。

    类似

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

    编辑:

    您可以将加载的事件处理程序添加到组合框,然后从后面的代码中找到视图模型并设置选定项属性。

        private void ComboBox_Loaded(object sender, RoutedEventArgs e)
        {
            ComboBox comboBox = sender as ComboBox;
            comboBox.SelectedItem =
                _viewModel.UserAccounts.Where(x => x.UserAccountString ==  _viewModel.SelectedUserAccount.UserAccountString);
        }
    

    【讨论】:

    • 为了清楚起见,您是否在重新创建 ui 实例的场景中尝试过此操作?就像您选择一个值并移动到不同的 xaml 页面并返回到此一样,或者您是否尝试选择一个值并且该值在组合框中显示为选中状态?我绝对可以提供一个更简单的场景。
    • 我没有测试导航。如果您正在导航,您可以在页面上设置 NavigationCacheMode="Enabled" 吗?
    • 这是在 SettingsFlyout 上,因此没有 NavigationCacheMode。我也有两个切换按钮,可以持久保存数据更改。我想真正的问题是:组合框在代码中设置时不会显示所选项目,如果所选项目实例与绑定到组合框的集合中包含的不同,则可能会发生这种情况,但我确保下次打开弹出按钮时,我会从集合中获取正确的实例。
    • 可以了,不知道为什么SelectedItem Binding不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 2013-12-08
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    相关资源
    最近更新 更多