<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();
}
}
我希望这个能帮上忙