0

ViewModel:すべてを選択したいPersons(各人はTypeEmployee 、 Manager または Customer である可能性があり、に存在する個別のコレクションですPersonTypePersons)、これは私がそれをさせたい唯一の操作ですViewModel

View:ComboBoxPersonTypesと の のDataGrid選択に基づいてフィルター処理されるが必要PersonTypeですComboBox(別の言葉で言えComboBoxば、 を の責任者にDataGrid Groupingしたいのですが) ですべてを行いXAMLます。

助言がありますか?

ありがとうございました。

4

2 に答える 2

3
    <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition Height="250"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="200"/>
        </Grid.ColumnDefinitions>
    <ComboBox x:Name="combo" ItemsSource="{Binding PersonTypes}" Tag="{Binding Persons}"/>
    <DataGrid  Grid.Row="1" AutoGenerateColumns="False">
        <DataGrid.ItemsSource>
            <MultiBinding Converter="{StaticResource conv}">
                <Binding Path="Tag" ElementName="combo"/>
                <Binding Path="SelectedItem" ElementName="combo"/>
            </MultiBinding>
        </DataGrid.ItemsSource>
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}"/>
        </DataGrid.Columns>
    </DataGrid>

</Grid>

    public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel();
    }
}
public class ViewModel
{
    public ViewModel()
    {
        PersonTypes = new ObservableCollection<PersonType>() { PersonType.Manager, PersonType.Customer, PersonType.Employee };

        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 });
    }
    public ObservableCollection<Person> Persons { get; set; }
    public ObservableCollection<PersonType> PersonTypes { get; set; }

}
public class Person
{
    public string Name { get; set; }
    public PersonType Type { get; set; }
}

public enum PersonType
{
    Manager = 0,
    Employee = 1,
    Customer = 2
}
public class MyConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (values.Count() > 1 && values[1] !=null && values[0] is ObservableCollection<Person>)
        {
            string ptype = values[1].ToString();
            ObservableCollection<Person> persons = (ObservableCollection<Person>)values[0];
            if (ptype != null && persons != null)
            {
                return persons.Where(p => p.Type.ToString() == ptype).ToList();
            }
        }

        return null;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

これが役立つことを願っています。

于 2012-07-26T06:46:56.467 に答える
2

完全を期すために、コンバーターを使用しない非(最小)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}"/>
于 2012-07-26T07:19:12.953 に答える