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

16 篇文章 0 订阅
12 篇文章 0 订阅

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;

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值