完全を期すために、コンバーターを使用しない非(最小)xamlソリューションを示します。
xaml:
<Grid>
<Grid.Resources>
<ObjectDataProvider x:Key="srcPersonType" MethodName="GetValues" ObjectType="{x:Type System:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:PersonType"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ComboBox ItemsSource="{Binding Source={StaticResource srcPersonType}}" SelectedItem="{Binding SelectedType}"/>
<DataGrid Grid.Row="1" ItemsSource="{Binding MyView}"/>
</Grid>
ビューモデル:
public class ViewModel
{
public ObservableCollection<Person> Persons { get; set; }
public ICollectionView MyView { get; set; }
private PersonType _selectedType;
public PersonType SelectedType
{
get { return _selectedType; }
set { _selectedType = value; this.MyView.Refresh(); }
}
public ViewModel()
{
Persons = new ObservableCollection<Person>();
Persons.Add(new Person() { Name = "ABC", Type = PersonType.Manager });
Persons.Add(new Person() { Name = "DEF", Type = PersonType.Manager });
Persons.Add(new Person() { Name = "GHI", Type = PersonType.Customer });
Persons.Add(new Person() { Name = "JKL", Type = PersonType.Manager });
Persons.Add(new Person() { Name = "MNO", Type = PersonType.Employee });
this.MyView = CollectionViewSource.GetDefaultView(this.Persons);
this.MyView.Filter = (item) =>
{
var person = (Person) item;
if (this.SelectedType == null || this.SelectedType == PersonType.All)
return true;
if (person.Type == this.SelectedType)
return true;
return false;
};
}
}
列挙型としてのpersontype:
public enum PersonType
{
All,
Manager,
Customer,
Employee
}
編集:コメントからの要件に合うように
ビューモデル
public IEnumerable<PersonType> MyGroup { get { return this.Persons.Select(x => x.Type).Distinct(); } }
//when ever your Person collection is altered, you have to call OnPropertyChanged("MyGroup")
xaml
<ComboBox ItemsSource="{Binding MyGroup}" SelectedItem="{Binding SelectedType}" />
<DataGrid Grid.Row="1" ItemsSource="{Binding MyView}"/>