【问题标题】:ComboBox SelectedIndex MVVM WPF组合框 SelectedIndex MVVM WPF
【发布时间】:2014-03-30 23:20:58
【问题描述】:

我有一个绑定到 tbPublications 的 ObservableCollection 的 ComboBox,它会按应有的方式填充。然后我从 DataGrid 中选择一行,该行触发另一个 Create 表单,在该表单中我将一条新记录插入tbPublications,一切都很好。

当我关闭所说的创建表单并返回到我的 ComboBox 表单时,我正在清除并重新阅读 ObservableCollection 的一个新项目,将用户返回到他们刚刚创建的项目。然后 ComboBox 会显示我新填充的集合中的一项,一切都很好。

我的问题是,在返回我的 ComboBox 表单时,新出版物未设置为 ComboBox 显示中的选定项目,用户必须单击 ComboBox 然后选择该项目。

我不能在我的视图 XAML 中使用 SelectedIndex = "0",因为我想在页面加载时在我的 ComboBox 中显示整个 ObservableCollection

有什么方法可以在 ViewModel 中使用方法来解决这个问题,比如..

      private void SetSelectedIndex()
      {
        if (MyObservableCollection.Count == 1)
        {
            //Set selected indexer to "0";
        }
      }

找到了解决方案,不确定它是否是最干净的“MVVM”解决方案...

在阅读了我的 ObservableCollection 之后,我调用了这个方法:

 if (_ModelPublicationsObservableList.Count == 1)
                {
                    SelectedPublication = _ModelPublication;
                    SetSelectedIndex();
                }

这是获取当前主窗口并设置 SelectedIndex 的方法:

 private void SetSelectedIndex()
    {
        ArticlesDataGridWindow singleOrDefault = (ComboBoxWindow)Application.Current.Windows.OfType<ComboBoxWindow>().SingleOrDefault(x => x.IsLoaded);
        singleOrDefault.comboBox1.SelectedIndex = 0;        
    }

【问题讨论】:

    标签: c# wpf xaml mvvm combobox


    【解决方案1】:

    您是否考虑过使用组合框的 SelectedItem 属性?可以绑定combobox的selected item属性来获取或设置选中的item。

    XAML

    <ComboBox ItemsSource="{Binding Path=Publications}" SelectedItem="{Binding Path=SelectedPublication, Mode=TwoWay}" />
    

    视图模型

    public class ItemListViewModel
    {
        public ObservableCollection<Publication> Publications {get; set;}
    
        private Publication _selectedPublication;
        public Publication SelectedPublication 
        {
            get { return _selectedPublication; }
            set
            {
                if (_selectedPublication== value) return;
                _selectedPublication= value;
                RaisePropertyChanged("SelectedPublication");
            }
        }
    }
    

    如果你想从 View 模型中设置选中项,你可以将 SelectedPublication 属性设置为-

    SelectedPublication = Publications[0];
    

    或者您可以在 Publications 集合中找到所需的项目并将其分配给 SelectedPublication 属性。

    【讨论】:

    • +1。这几乎总是要走的路,除非概念上的“选定值”是ComboBox 中一个项目的属性(而不是项目本身),在这种情况下一个可以使用SelectedValueSelectedValuePath。在关注 MVVM 时,几乎没有理由使用 SelectedIndex
    • 是的,我的 ViewModel 中有一个 SelectedPublication 属性。我的 XAML 设置为 'SelectedItem="{Binding SelectedPublication, Mode=TwoWay}"' 但 ComboBox 不显示 ObservableCollection 中的 1 行,我仍然需要单击下拉菜单并选择它...
    • @Hardgraf:您可能需要从视图模型中设置 SelectedPublication 属性以设置选定项。
    【解决方案2】:

    UpdateSourceTrigger=PropertyChanged 添加到您的绑定中。

    SelectedItem={Binding Path=SelectedPublication, Mode=TwoWay}, UpdateSourceTrigger=PropertyChanged}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      • 2018-05-03
      • 2017-02-02
      • 2015-09-15
      相关资源
      最近更新 更多