您的位置:首页 > 其它

MultiBinding 实现动态传入参数 Combox选择性显示数据

2013-08-12 15:58 387 查看
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;

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: