本篇小编就为大家准备了一片WPF中Combobox的控件总结,实例:
Combobox常用属性:
isEditable | 文本是否编辑(ture/false) |
isreadonly | 是否为只读属性(ture/false) |
Text | 文本(也可以当做默认值使用) |
后台添加数据
// 1 使用items属性进行添加
this.c1.Items.Add("贺钓帝");
this.c1.Items.Add("冰心");
this.c1.Items.Add("周树人");
// clear();清空属性
this.c1.Items.Cl属性
数据绑定
使用datacontext进行绑定
// 班级模型类
public class StudentClass
{
public int StudentId { get; set; }
public string Name { get; set; }
}
List<StudentClass> values = new List<StudentClass>();
values.Add(new StudentClass()
{
StudentId = 1,
Name = "软件1班",
});
// 需要再标签上添加数据绑定 ItemsSource="{Binding}
this.c1.ItemsSource = values; // 设置数据源
this.c1.DisplayMemberPath = "Name"; // 设置显示成员
this.c1.SelectedValuePath = "StudentId"; // 获取selectvalue值,获取的是sel
在后台获取用户选中的值
// 获取selectvalue值,获取的是selectedvaluePath的值
string str = c1.SelectedValue;
布局使用
<ComboBox IsEditable="True"
IsReadOnly="True"
Width="120"
Height="40"
VerticalAlignment="Top">
<!--设置子选项-->
<ComboBoxItem>ont</ComboBoxItem>
<ComboBoxItem>two</ComboBoxItem>
<ComboBoxItem>three</ComboBoxItem>
</ComboBox>