【问题标题】:ComboBox SelectedValue correctly sets SelectedValue in Viewmodel but not in ViewComboBox SelectedValue 在 Viewmodel 中正确设置 SelectedValue 但在 View 中没有
【发布时间】:2012-11-01 06:34:02
【问题描述】:

在我的项目中,我的 selectedvalue 在 vi​​ewmodel 中正确设置,但我的视图没有设置其 selectedvalue

在 xaml 代码中:

 <ComboBox ItemsSource="{Binding AllValues}" SelectedValue="{Binding SelectedValue,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="150" Margin="5,0,0,0">

在 ViewModel 中:

public Model SelectedValue
        {
            get
            {
                return _model.Value;
            }
            set
            {
                 _model.Value = value;
                if (CVSCollection.View != null)
                    CVSCollection.View.Refresh();
                RaisePropertyChanged("SelectedValue");
            }
        }

【问题讨论】:

    标签: wpf combobox collectionviewsource selectedvalue icollectionview


    【解决方案1】:

    尝试将ICollectionViewIsSynchronizedWithCurrentItem 结合使用。 ICollectionView 负责所有与组合框相关的操作。您也可以设置当前选中的项目。

    Xaml:

      <ComboBox ItemsSource="{Binding AllValues}"  
                      IsSynchronizedWithCurrentItem="True"
                      Width="150" 
                      Margin="5,0,0,0"/>  
    

    视图模型

    using System;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows.Data;
    
    namespace ComboBoxBinding
    {
        public class ViewModelSpike : INotifyPropertyChanged
        {
            private ObservableCollection<Model> _allValues = new ObservableCollection<Model>();
            private Model _selectedValue;
    
            public ObservableCollection<Model> AllValues
            {
                get { return _allValues; }
                set { _allValues = value; OnPropertyChanged("AllValues");}
            }
    
            public Model SelectedValue
            {
                get { return _selectedValue; }
                set { _selectedValue = value; OnPropertyChanged("SelectedValue");}
            }
    
            public ICollectionView ModelsView { get; private set; }
    
            public ViewModelSpike()
            {
                //First init / load AllValues here...
    
                ModelsView = CollectionViewSource.GetDefaultView(AllValues);
                ModelsView.CurrentChanged += OnCurrentModelChanged;
    
                //You can also set the current selected item / initial selected item
                //ModelsView.MoveCurrentTo(DesiredModel from AllValues)
            }
    
            private void OnCurrentModelChanged(object sender, EventArgs e)
            {
                if (ModelsView.CurrentItem == null) return;
                var selectedValue = ModelsView.CurrentItem as Model;
    
                if (selectedValue == null) return;
    
                SelectedValue = selectedValue;
    
                if (CVSCollection.View != null) CVSCollection.View.Refresh();
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected virtual void OnPropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    

    【讨论】:

    • hey thnx for reply...我已经尝试过了,但它不起作用,并且用它创建的另一个问题是 CVSCollection.View.Refresh() not refresh my view..@menty
    • 嗯,奇怪。您是否已在 OnCurrentModelChanged 方法中设置断点以查看模型视图是否正确连接?在将模型视图附加到它之前,您是否已经设置/加载了 AllValues 集合?您是否已将组合框中的 IsSynchronizedWithCurrentItem 属性设置为 true?我在回答中描述的方法是一种常见的方式,我们像这样连接所有的组合框、列表框等。
    • 我自己解决了这个问题,因为我已经为 SelectedValue 设置了本地变量,然后在构造函数中设置了 _selectedValue = _model.value 的值..
    【解决方案2】:

    我已经自己解决了,因为我将 selectedvalue 的局部变量设置为 _selectedValue,并且在构造函数中我设置了 _selectedValue..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-02
      • 2021-03-26
      • 2018-01-09
      • 2023-04-01
      • 2011-09-10
      • 1970-01-01
      • 2018-05-24
      • 1970-01-01
      相关资源
      最近更新 更多