0

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

2 Answers2

0

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));
        }
    }
}
menty
  • 416
  • 3
  • 6
  • hey thnx for reply...I have tried this but its not working for and another issue created with it is CVSCollection.View.Refresh() not refresh my view..@menty –  Nov 02 '12 at 09:06
  • hmmm, strange. Do you have set a breakpoint into the OnCurrentModelChanged method to see if the modelsview is correct wired? Do you have setup / load the AllValues Collection before attaching the modelsview to it? Do you have set the IsSynchronizedWithCurrentItem property from the combobox to true? The method I descried in my answer is a common way, we wire all our comboboxes, listboxes, etc. like this. – menty Nov 02 '12 at 09:38
  • I have solved it by my own as i have set local varible for SelectedValue and then into Constructor set the value of _selectedValue = _model.value.. –  Nov 07 '12 at 07:39
0

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