2012-09-14 56 views
3

我有一个WPF窗口应用程序项目,它有一个窗口有两个ListBox控件,问题是我如何绑定一个源到这两个控件? 源就像是:WPF:两个控件绑定到一个源,如何过滤绑定?

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

ObservableCollection<student> m_myGroup; 

我想:ListBox1的结合m_myGroup如果年龄> 25 ListBox2如果年龄结合m_myGroup < = 25 显然,无论这两个列表框有

TextBlock Text={binding Path=name} 

我不想使用DataTrigger将项目可见性属性隐藏或显示,我尝试使用ICollectionView来过滤源,但它会影响其他ListBox! 有谁知道如何为每个ListBox制作两个过滤器,并且他们只绑定到一个源?

回答

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

我希望这将有助于

+0

非常感谢,作为改进,我也想知道: \t公共类学生 \t { \t \t公共字符串名称{;组; } \t \t public int Age {get;组; } \t \t ObservableCollection m_myTeam; \t} 如何过滤? – leoyang99

+0

\t公共类学生 \t { \t \t公共字符串名称{;设置;} \t \t公众诠释年龄{获取;集;} \t \t ObservableCollection m_myTeam; \t} 如何过滤这个? – leoyang99

2

为m_myGroup创建两个ICollectionView,对它们进行过滤并将其绑定到ListBoxes

对于这个问题,将ListBox.IsSynchronizedWithCurrentItem设置为false,选择时ListBoxes之间不会有影响。

请参阅this

编辑:

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;}; 
    } 
} 

此绑定之后,ViewSourcesListBoxes

+0

非常感谢,但我想知道详细信息,如何创建两个ICollecntionView并绑定到这些ListBox? – leoyang99

+0

查看本文关于ICollectionView ..其非常简单http://marlongrech.wordpress.com/2008/11/22/icollectionview-explained/ – Sandepku