WPF控件之ComboBox
来源:互联网 发布:网络造谣举报 编辑:程序博客网 时间:2024/05/29 20:00
WPF ComboBox
WPF ComboBox
创建一个ComboBox控件,并设置ComboBox控件的名称,高度,宽度。及设置ComboBox的垂直和水平对齐。
<ComboBox Name="ComboBox1" Width="200" Height="30" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,10,0,0"></ComboBox>
输出结果如图所示:
添加ComboBox项
<ComboBox Name="ComboBox1" Width="200" Height="30" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,10,0,0">
<ComboBoxItem Content="太原"></ComboBoxItem>
<ComboBoxItem Content="北京" IsSelected="True"></ComboBoxItem>
<ComboBoxItem Content="石家庄"></ComboBoxItem>
<ComboBoxItem Content="哈尔滨"></ComboBoxItem>
<ComboBoxItem Content="合肥"></ComboBoxItem>
<ComboBoxItem Content="南京"></ComboBoxItem>
</ComboBox>
IsSelected属性为ComboxBox中的默认选中项
输出结果如图所示:
在运行时添加和删除ComboBox项
XAML
<StackPanel Orientation="Horizontal" Height="40" VerticalAlignment="Top">
<ComboBox Name="ComboBox1" Width="100" Margin="5" VerticalContentAlignment="Center">
<ComboBoxItem Content="太原"></ComboBoxItem>
<ComboBoxItem Content="北京" ></ComboBoxItem>
<ComboBoxItem Content="石家庄"></ComboBoxItem>
<ComboBoxItem Content="哈尔滨"></ComboBoxItem>
<ComboBoxItem Content="合肥"></ComboBoxItem>
<ComboBoxItem Content="南京"></ComboBoxItem>
</ComboBox>
<TextBox Width="100" Margin="5" x:Name="ItemNames" VerticalContentAlignment="Center"></TextBox>
<Button Margin="5" Content="添加" x:Name="Add" Width="60" Click="Add_Click"></Button>
<Button Margin="5" Content="删除" x:Name="Delete" Width="60" Click="Delete_Click"></Button>
</StackPanel>
CS
private void Add_Click(object sender, RoutedEventArgs e)
{
if(!string.IsNullOrEmpty(ItemNames.Text))
ComboBox1.Items.Add(ItemNames.Text);
}
private void Delete_Click(object sender, RoutedEventArgs e)
{
ComboBox1.Items.RemoveAt(ComboBox1.Items.IndexOf(ComboBox1.SelectedItem));
}
输出结果如图所示:
修改ComboBoxItem样式
<ComboBox Name="ComboBox1" Width="100" Margin="5" VerticalContentAlignment="Center">
<ComboBoxItem Content="太原" Background="LightGray" Foreground="Black" FontFamily="Georgia" FontSize="14" FontWeight="Bold"></ComboBoxItem>
<ComboBoxItem Content="北京" Background="LightBlue" Foreground="Purple" FontFamily="Verdana" FontSize="12" FontWeight="Bold"></ComboBoxItem>
<ComboBoxItem Content="石家庄" Background="LightGreen" Foreground="Green" FontFamily="Georgia" FontSize="14" FontWeight="Bold"></ComboBoxItem>
<ComboBoxItem Content="哈尔滨" Background="LightBlue" Foreground="Blue" FontFamily="Verdana" FontSize="12" FontWeight="Bold"></ComboBoxItem>
<ComboBoxItem Content="合肥" Background="LightSlateGray" Foreground="Orange" FontFamily="Georgia" FontSize="14" FontWeight="Bold"></ComboBoxItem>
</ComboBox>
输出结果如图所示:
在ComboBoxItem项中显示图片
<ComboBoxItem Background="LightGray" Foreground="Black" FontFamily="Georgia" FontSize="14" FontWeight="Bold">
<StackPanel Orientation="Horizontal">
<Image Source="Image\12.jpg" Height="30" Width="30"></Image>
<TextBlock Text="太原" VerticalAlignment="Center"></TextBlock>
</StackPanel>
</ComboBoxItem>
输出结果如图所示:
将复选框添加到ComboBoxItem
<ComboBoxItem Background="LightGray" Foreground="Black" FontFamily="Georgia" FontSize="14" FontWeight="Bold">
<CheckBox Name="TaiYuanCheckBox">
<StackPanel Orientation="Horizontal">
<Image Source="Image\12.jpg" Height="30" Width="30"></Image>
<TextBlock Text="太原" VerticalAlignment="Center"></TextBlock>
</StackPanel>
</CheckBox>
</ComboBoxItem>
输出结果如图所示:
获取当前选定项的值
string str= ComboBox1.SelectedItem.ToString();
数据绑定
public class Info { public int Id { get; set; } public string Name { get; set; } } //绑定用户类型 private void bindUserType() { IList<Info> list = new List<Info>() { new Info(){ Id=0, Name="供应商"}, new Info(){ Id=1, Name="bbbbbb"} }; cobxUserType.ItemsSource = list; cobxUserType.SelectedValuePath = "Id"; cobxUserType.DisplayMemberPath = "Name"; }
- WPF控件之ComboBox
- WPF ComboBox控件
- wpf 之ComboBox的用法
- WPF 自定义带有数据源的combobox控件
- wpf ComboBox 控件利用DataSet数据绑定
- VC 控件 之 Combobox
- VC 控件 之 Combobox
- MFC之ComboBox控件
- MFC之ComboBox控件
- WPF ComboBox
- VB.NET控件之ComboBox
- easyui控件之combobox用法。
- MFC之ComboBox控件用法
- WPF之控件开发
- WPF控件之ImageButton
- WPF之布局控件
- WPF之自定义控件
- WPF 控件之 Popup
- 第十四周 项目1-动物这样叫
- android 通过网络调试
- DSP、ARM和单片机的区别
- Tomcat内存、连接数等性能参数设置
- ACM之路——大一省赛
- WPF控件之ComboBox
- JNI demo 手把手教例子
- Java利用串口编程技术操作继电器
- Android学习日志08__五大布局02__Absolute&Frame&TableLayout
- 启用fcitx-qimpanel面板程序
- How to Install MySQL 5.6 on CentOS 6.3/RedHat EL6/Fedora
- Qt为啥从4.8直接就跳到5.3了呢?这不科学吧
- JFreeChart自定义Y轴数值单位
- Android程序打包成apk文件