2012-09-14 58 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