C# WPF:两个控件绑定到一个源,如何过滤绑定?

C# WPF:两个控件绑定到一个源,如何过滤绑定?,c#,wpf,C#,Wpf,我有一个WPF windows应用程序项目,它有一个带有两个ListBox控件的窗口,问题是如何将一个源绑定到这两个控件? 来源如下: class student { public string name{get;set;} public int age{get;set;} } ObservableCollection<student> m_myGroup; 我不想使用DataTrigger隐藏或显示item visibility属性,我尝试使用ICollecti

我有一个WPF windows应用程序项目,它有一个带有两个ListBox控件的窗口,问题是如何将一个源绑定到这两个控件? 来源如下:

class student
{
    public string name{get;set;}
    public int age{get;set;}
}

ObservableCollection<student> m_myGroup;
我不想使用DataTrigger隐藏或显示item visibility属性,我尝试使用ICollectionView过滤源,但它会影响其他列表框!
有人知道如何为每个列表框创建两个过滤器,并且它们只绑定到一个源吗?

为m_myGroup创建两个
ICollectionView
,过滤它们并将它们绑定到
列表框

因此,如果将
列表框.IsSynchronizedWithCurrentItem
设置为false,则在选择时,
列表框之间不会产生任何影响

编辑:

public class StudentHandler
{
    ObservableCollection<student> m_myGroup;

    public CollectionViewSource YoungStudentsViewSource { get; private set; }
    public CollectionViewSource OldStudentsViewSource { get; private set; }

    public StudentHandler
    {
        YoungStudentsViewSource = new CollectionViewSource {Source = m_myGroup};
        OldStudentsViewSource = new CollectionViewSource {Source = m_myGroup}; 

        YoungStudentsViewSource.Filter = (stud) => {return (stud as student).age<=25;};
        OldStudentsViewSource .Filter = (stud) => {return (stud as student).age>25;};
    }
}
public class StudentHandler
{
可观察收集m_myGroup;
公共集合ViewSource YoungStudentsViewSource{get;private set;}
公共集合ViewSource OldStudentsViewSource{get;private set;}
公共学生管理员
{
YoungStudentsViewSource=newcollectionviewsource{Source=m_myGroup};
OldStudentsViewSource=newcollectionviewsource{Source=m_myGroup};
YoungStudentsViewSource.Filter=(stud)=>{return(stud作为学生)。age{return(stud作为学生)。age>25;};
}
}
在此之后,将
视图源
绑定到
列表框


<Window x:Class="ComboboxStyle.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:converter="clr-namespace:ComboboxStyle"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <converter:AgeConverter x:Key="ageConv"/>
</Window.Resources>

<Grid >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <ListBox Grid.Column="0"  DisplayMemberPath="Name" ItemsSource="{Binding Students, Converter={StaticResource ageConv}, ConverterParameter=agelessthan25}" >
    </ListBox>
    <ListBox  Grid.Column="1" DisplayMemberPath="Name" ItemsSource="{Binding Students, Converter={StaticResource ageConv}, ConverterParameter=agegreaterthan25}" >
    </ListBox>
</Grid>

公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
学生=新的可观察集合();
添加(新学生(){Name=“Aeqwwe”,年龄=24});
Add(new Student(){Name=“bqwewqeq”,Age=28});
添加(新学生(){Name=“cwqeqw”,年龄=23});
Add(new Student(){Name=“dweqw”,Age=29});
Add(new Student(){Name=“eqweq”,Age=20});
DataContext=this;
}
公共可观察集合学生{get;set;}
}
公立班学生
{
公共字符串名称{get;set;}
公共整数{get;set;}
}
公共类AgeConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
var项目=作为可观察集合的价值;
if(参数!=null&&items!=null)
{
if(parameter.ToString()=“agelessthan25”)
{
返回项目。其中(i=>i.Age<25.ToList();
}
else if(parameter.ToString()=“年龄大于25”)
{
返回项目。其中(i=>i.Age>=25)。ToList();
}
}
返回null;
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}

我希望这会有所帮助

非常感谢,但我想知道细节,如何创建两个ICollectionView并绑定到这些列表框?请查看这篇关于ICollectionView的文章。非常简单,非常感谢,作为改进,我还想知道:
<Window x:Class="ComboboxStyle.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:converter="clr-namespace:ComboboxStyle"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <converter:AgeConverter x:Key="ageConv"/>
</Window.Resources>

<Grid >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <ListBox Grid.Column="0"  DisplayMemberPath="Name" ItemsSource="{Binding Students, Converter={StaticResource ageConv}, ConverterParameter=agelessthan25}" >
    </ListBox>
    <ListBox  Grid.Column="1" DisplayMemberPath="Name" ItemsSource="{Binding Students, Converter={StaticResource ageConv}, ConverterParameter=agegreaterthan25}" >
    </ListBox>
</Grid>
 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Students = new ObservableCollection<Student>();
        Students.Add(new Student() { Name = "Aeqwwe", Age = 24 });
        Students.Add(new Student() { Name = "bqwewqeq", Age = 28 });
        Students.Add(new Student() { Name = "cwqeqw", Age = 23 });
        Students.Add(new Student() { Name = "dweqqw", Age = 29 });
        Students.Add(new Student() { Name = "eqweweq", Age = 20 });
        DataContext = this;
    }
    public ObservableCollection<Student> Students { get; set; }
}
public class Student
{
    public string Name { get; set; }
    public int Age { get; set; }
}
public class AgeConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var items = value as ObservableCollection<Student>;
        if (parameter != null && items != null)
        {
            if (parameter.ToString() == "agelessthan25")
            {
                return items.Where(i => i.Age < 25).ToList();
            }
            else if (parameter.ToString() == "agegreaterthan25")
            {
                return items.Where(i => i.Age >= 25).ToList();
            }
        }
        return null;
    }

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