This project has moved. For the latest updates, please go here.

How can i use CheckComboBox with MVVM

Jun 6, 2014 at 5:36 AM
I want to use CheckComboBox with MVVM and i want to get all checked items in a observable collection,also get Checked and unchecked event of items on View Model.
How can i achieve these functionality of CheckComoboBox with MVVM ?
Developer
Jun 6, 2014 at 6:02 PM
Hi,

You could try the following :
View.xaml :
<xctk:CheckComboBox x:Name="_checkComboBox"
                          ItemsSource="{Binding Countries}"
                          DisplayMemberPath="Name"
                          SelectedItemsOverride="{Binding SelectedCountries}"
                          Width="200"
                          Height="25">
</xctk:CheckComboBox>

View.cs:
public partial class View : UserControl
  {
    public View()
    {
      InitializeComponent();
    }
  }

ViewModel.cs:
public class ViewModel : DependencyObject
  {
    private readonly Model _model = new Model();

    public ViewModel()
    {
      ObservableCollection<Country> europeCountries = new ObservableCollection<Country>()
      {
        new Country() { Name = "Germany", Population = 6 },
        new Country() { Name = "Great Britain", Population = 12 },
        new Country() { Name = "France", Population = 7 }
      };
      this.Countries = europeCountries;

      this.SelectedCountries = new ObservableCollection<Country>() { this.Countries[ 1 ] };
      this.SelectedCountries.CollectionChanged += this.SelectedCountries_CollectionChanged;
    }

    private void SelectedCountries_CollectionChanged( object sender, NotifyCollectionChangedEventArgs e )
    {
      //Something is checked or unchecked
      _model.DoStuff();
    }

    #region Countries

    public static readonly DependencyProperty CountriesProperty = DependencyProperty.Register( "Countries", typeof( ObservableCollection<Country> ), typeof( ViewModel )
                                                                                         , new UIPropertyMetadata( null, OnCountriesChanged ) );
    public ObservableCollection<Country> Countries
    {
      get
      {
        return ( ObservableCollection<Country> )GetValue( CountriesProperty );
      }
      set
      {
        SetValue( CountriesProperty, value );
      }
    }

    private static void OnCountriesChanged( DependencyObject o, DependencyPropertyChangedEventArgs e )
    {
      ViewModel viewModel = o as ViewModel;
      if( viewModel != null )
        viewModel.OnCountriesChanged( ( ObservableCollection<Country> )e.OldValue, ( ObservableCollection<Country> )e.NewValue );
    }

    protected virtual void OnCountriesChanged( ObservableCollection<Country> oldValue, ObservableCollection<Country> newValue )
    {
    }

    #endregion

    #region SelectedCountries

    public static readonly DependencyProperty SelectedCountriesProperty = DependencyProperty.Register( "SelectedCountries", typeof( ObservableCollection<Country> ), typeof( ViewModel )
                                                                                         , new UIPropertyMetadata( null, OnSelectedCountriesChanged ) );
    public ObservableCollection<Country> SelectedCountries
    {
      get
      {
        return ( ObservableCollection<Country> )GetValue( SelectedCountriesProperty );
      }
      set
      {
        SetValue( SelectedCountriesProperty, value );
      }
    }

    private static void OnSelectedCountriesChanged( DependencyObject o, DependencyPropertyChangedEventArgs e )
    {
      ViewModel viewModel = o as ViewModel;
      if( viewModel != null )
        viewModel.OnSelectedCountriesChanged( ( ObservableCollection<Country> )e.OldValue, ( ObservableCollection<Country> )e.NewValue );
    }

    protected virtual void OnSelectedCountriesChanged( ObservableCollection<Country> oldValue, ObservableCollection<Country> newValue )
    {
      _model.DoStuff();
    }

    #endregion
  }

  public class Country
  {
    public string Name
    {
      get;
      set;
    }

    public int Population
    {
      get;
      set;
    }
  }

Model.cs :
public class Model
  {
    public void DoStuff()
    {
    }
  }

MainWindow.xaml ;
<Window x:Class="WpfApplication69.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication69"
        Title="MainWindow" Height="350" Width="525">
   <Window.DataContext>
      <local:ViewModel />
   </Window.DataContext>

   <Grid>
      <local:View />
   </Grid>
</Window>
Jun 7, 2014 at 7:32 AM
Thanks a lot BoucherS ,
It works for me,I did all the things expect selected values in CheckComboBox When it loads first time
Your reply resolved my issue :)