WPF:两个控件绑定同一数据源,如何过滤绑定?

3
我是一位有用的助手,可以为您进行翻译。以下是您需要翻译的内容:

我有一个 WPF 窗口应用程序项目,其中包含一个窗口和两个 ListBox 控件。我的问题是如何将一个源绑定到这两个控件?该源如下:

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

ObservableCollection<student> m_myGroup;

我希望:如果年龄>25,将ListBox1绑定到m_myGroup;如果年龄<=25,则将ListBox2绑定到m_myGroup。显然,这两个ListBox都有一个

标签。
TextBlock Text={binding Path=name}

我不想使用DataTrigger来控制项的可见性属性隐藏或显示,我尝试使用ICollectionView来过滤源,但这会影响到其他ListBox!有人知道如何为每个ListBox创建两个过滤器,并且它们只绑定到一个源吗?
2个回答

2
创建两个 ICollectionView 用于 m_myGroup,对它们进行筛选并将其绑定到 ListBoxes 上。
为此,将 ListBox.IsSynchronizedWithCurrentItem 设置为 false 将不会在选择时影响 ListBoxes 之间的关系。
请参见此链接编辑:
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;};
    }
}

在此之后,将ViewSources绑定到ListBoxes上。


非常感谢,但我想知道详细信息,如何创建两个ICollecntionView并将其绑定到这些ListBox? - leoyang99
请查看关于ICollectionView的这篇文章。它非常简单易懂。http://marlongrech.wordpress.com/2008/11/22/icollectionview-explained/ - Sandepku

0
<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();
    }
}

希望这能有所帮助


非常感谢,作为改进,我还想知道: public class Student { public string Name { get; set; } public int Age { get; set; } ObservableCollection m_myTeam; } 如何过滤它? - leoyang99
<TreeViewItem.ItemTemplate> <HierarchicalDataTemplate x:Name="m_TemplateStudentItem" ItemsSource="{Binding m_myTeam}"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Name}"/> </StackPanel> </HierarchicalDataTemplate>
</TreeViewItem.ItemTemplate> public class Student { public string Name { get; set; } public int Age { get; set; } ObservableCollection<Student> m_myTeam; } 如何进行筛选?
- leoyang99

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接