XAML:
<Grid x:Name="LayoutRoot">
<ComboBox HorizontalAlignment="Left" Text="Name" Name="CbBz" Margin="31,171,0,0" VerticalAlignment="Top" Width="120"/>
<ComboBox x:Name="Cm" HorizontalAlignment="Left" Margin="31,214,0,0" VerticalAlignment="Top" Width="120">
<ComboBox.ItemsSource>
<MultiBinding Converter="{StaticResource MulitConvert}">
<Binding />
<Binding ElementName="CbBz" Path="Text"/>
</MultiBinding>
</ComboBox.ItemsSource>
</ComboBox>
</Grid>
CS:
public class MulitConvert : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
List<Student> Lt = (List<Student>)values[0];
switch (values[1].ToString())
{
case "Name":
var v = from n in Lt select n.Name;
return v;
case "Age":
return from n in Lt select n.Age;
}
return new object();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
List<Student> Lt = new List<Student>();
CbBz.Items.Add("Name");
CbBz.Items.Add("Age");
Student S1 = new Student();
S1.Name = "张飞";
S1.Age = 1000;
Student S2 = new Student();
S2.Name = "赵云";
S2.Age = 2000;
Student S3 = new Student();
S3.Name = "刘备";
S3.Age = 3000;
Lt.Add(S1);
Lt.Add(S2);
Lt.Add(S3);
LayoutRoot.DataContext = Lt;