Question

In my Project my selectedvalue sets correctly in viewmodel but my view not sets its selectedvalue

in xaml code:

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

in ViewModel:

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

Solution 2

I have solved by my own as i have set local variable for selectedvalue as _selectedValue and into constructor i have set _selectedValue..

OTHER TIPS

Try using a ICollectionView in combination with IsSynchronizedWithCurrentItem. The ICollectionView takes care of all your combobox related actions. You could also set the current selected item.

Xaml:

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

ViewModel

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));
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top