微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SilverLight 学习笔记一之ComboBox

1,ComboBox简单 绑定

 <ComboBox Grid.Row="1" Grid.Column="3"  Height="20" HorizontalAlignment="Left"  VerticalAlignment="Top" Name="lstSex" Width="100" Margin="5,4,0" Selectedindex="0">
    <ComboBoxItem Content="先生" />
    <ComboBoxItem Content="女士" />
</ComboBox>

2,ComboBox数据绑定

2.1 xmal

<ComboBox Grid.Row="0" Grid.Column="3" Height="20" HorizontalAlignment="Left"  VerticalAlignment="Top" Name="Comb1" Width="100" Margin="5,8,0">
</ComboBox>

2.2 数据源

public class Student
{
    public string Name
    {
        set;
        get;
    }
}

public class Students
{
    public List<Student> StudentList
    {
        set;
        get;
    }
	
    public List<Student> getStudent()
    {
        StudentList= new List<Student>  
            {
                new Student(){Name="X1"},new Student(){Name="X2"},new Student(){Name="X3"}
            };
        return StudentList;
    }
}

2.2 后台代码

private void BindStudent()
{
    Comb1.ItemsSource = new Students().getStudent();
    Comb1.displayMemberPath = "Name";
}

3  ComboBox赋值

3.1 ComboBox的Items处于固定的状态

Comb1.ItemsSource = new Students().getStudent();
switch (usermodel.UserDepartment )
{
    case "X1":
        this.Comb1 .Selectedindex =0;
        break;
    case "X2":
        this.Comb1 .Selectedindex =1;
        break;                    
    case "X3":
        this.Comb1 .Selectedindex =2;
        break;                                    
}

3.2  当ComboBox的Items时时处于变化的状态

数据库查询到student.Name,Name可能随时被修改

foreach(Student item in Comb1.Items)
{
    if(item.Name == student.Name)
     {
        Comb1.SelectedItem = item;
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐