Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 使用comboBox作为源对自动生成数据网格进行分组_Wpf_Data Binding_Mvvm_Filtering_Grouping - Fatal编程技术网

Wpf 使用comboBox作为源对自动生成数据网格进行分组

Wpf 使用comboBox作为源对自动生成数据网格进行分组,wpf,data-binding,mvvm,filtering,grouping,Wpf,Data Binding,Mvvm,Filtering,Grouping,视图模型: 我想选择所有人员(每个人可能属于类型:员工、经理或客户,是人员中存在的人员类型的独特集合) 这是我唯一想让ViewModel实现的操作 查看: 我想要一个PersonTypes的ComboBox和一个DataGrid,它是根据ComboBox的选定PersonTypes进行过滤的(换句话说,我想让ComboBox负责DataGrid分组),并在XAML中完成所有工作 有什么建议吗 谢谢。 <Grid> <Grid.RowDefinitions>

视图模型
: 我想选择所有
人员
(每个人可能属于
类型
:员工、经理或客户,是
人员中存在的
人员类型
的独特集合) 这是我唯一想让
ViewModel
实现的操作

查看
: 我想要一个
PersonTypes
ComboBox
和一个
DataGrid
,它是根据
ComboBox
的选定
PersonTypes
进行过滤的(换句话说,我想让
ComboBox
负责
DataGrid
分组
),并在
XAML
中完成所有工作

有什么建议吗

谢谢。


    <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();
    }
}
公共部分类主窗口:窗口 { 公共主窗口() { 初始化组件(); DataContext=新的ViewModel(); } } 公共类视图模型 { 公共视图模型() { PersonTypes=newobserveCollection(){PersonType.Manager,PersonType.Customer,PersonType.Employee}; 人员=新的可观察集合(); 添加(newperson(){Name=“ABC”,Type=PersonType.Manager}); 添加(newPerson(){Name=“DEF”,Type=PersonType.Manager}); 添加(newperson(){Name=“GHI”,Type=PersonType.Customer}); 添加(newperson(){Name=“JKL”,Type=PersonType.Manager}); 添加(newperson(){Name=“MNO”,Type=PersonType.Employee}); } 公共可观察集合人员{get;set;} 公共ObservableCollection PersonTypes{get;set;} } 公共阶层人士 { 公共字符串名称{get;set;} 公共PersonType类型{get;set;} } 公共枚举个人类型 { 经理=0, 雇员=1, 客户=2 } 公共类MyConverter:IMultiValueConverter { 公共对象转换(对象[]值,类型targetType,对象参数,System.Globalization.CultureInfo区域性) { 如果(values.Count()>1&&values[1]!=null&&values[0]是ObservableCollection) { 字符串ptype=值[1]。ToString(); ObservableCollection persons=(ObservableCollection)值[0]; if(ptype!=null&&persons!=null) { 返回persons.Where(p=>p.Type.ToString()==ptype.ToList(); } } 返回null; } 公共对象[]转换回(对象值,类型[]目标类型,对象参数,System.Globalization.CultureInfo区域性) { 抛出新的NotImplementedException(); } }
我希望这会有所帮助。


公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
DataContext=新的ViewModel();
}
}
公共类视图模型
{
公共视图模型()
{
PersonTypes=newobserveCollection(){PersonType.Manager,PersonType.Customer,PersonType.Employee};
人员=新的可观察集合();
添加(newperson(){Name=“ABC”,Type=PersonType.Manager});
添加(newPerson(){Name=“DEF”,Type=PersonType.Manager});
添加(newperson(){Name=“GHI”,Type=PersonType.Customer});
添加(newperson(){Name=“JKL”,Type=PersonType.Manager});
添加(newperson(){Name=“MNO”,Type=PersonType.Employee});
}
公共可观察集合人员{get;set;}
公共ObservableCollection PersonTypes{get;set;}
}
公共阶层人士
{
公共字符串名称{get;set;}
公共PersonType类型{get;set;}
}
公共枚举个人类型
{
经理=0,
雇员=1,
客户=2
}
公共类MyConverter:IMultiValueConverter
{
公共对象转换(对象[]值,类型targetType,对象参数,System.Globalization.CultureInfo区域性)
{
如果(values.Count()>1&&values[1]!=null&&values[0]是ObservableCollection)
{
字符串ptype=值[1]。ToString();
ObservableCollection persons=(ObservableCollection)值[0];
if(ptype!=null&&persons!=null)
{
返回persons.Where(p=>p.Type.ToString()==ptype.ToList();
}
}
返回null;
}
公共对象[]转换回(对象值,类型[]目标类型,对象参数,System.Globalization.CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}

我希望这会有所帮助。

为了完整起见,这里有一个没有转换器的非(最低)xaml解决方案

xaml:

编辑:符合注释中的要求

视图模型

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")
public IEnumerable MyGroup{get{返回this.Persons.Select(x=>x.Type).Distinct();}
//当您的Person集合发生更改时,您必须调用PropertyChanged(“MyGroup”)
xaml


为了完整起见,这里有一个不带转换器的非(最低)xaml解决方案

xaml:

编辑:符合注释中的要求

视图模型

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")
public IEnumerable MyGroup{get{返回this.Persons.Select(x=>x.Type).Distinct();}
//当您的Person集合发生更改时,您必须调用PropertyChanged(“MyGroup”)
xaml



你的意思是在编写分组时进行过滤吗?@blindmeis:我的意思是1=>按类型分组(并在中的
人员
中加载一个不同的
人员
列表
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")
    <ComboBox ItemsSource="{Binding MyGroup}" SelectedItem="{Binding SelectedType}" />
    <DataGrid Grid.Row="1"  ItemsSource="{Binding MyView}"/>