Hi,
You could try the following :
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>