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();
WPF ComboBox(转)的更多相关文章
- wpf ComboBox设置默认值
最新的wpf的ComboBox设置默认值得方法是,给VM中的数据集合第一个元素插入一个提示项目,比如:请选择一项,然后通过数据绑定可以实现默认选中第一项,下面我就贴一下示例代码: xaml页面: &l ...
- wpf ComboBox的SelectionBoxItem相关依赖属性
以前没有注意SelectionBoxItem相关依赖属性,这几天看wpf源码 特意研究了一番 <Style x:Key="ComboBoxStyle1" TargetType ...
- WPF combobox数据绑定和数据获取
本文章仅为个人学习,如有错误之处请指正. 搭建WPF界面的时候,想用combobox构建一个下拉菜单,搜索的时候看到大多数都是大段代码,逻辑顺序不是很详细,摸索了大概,记录一下方便来者. 拖入comb ...
- WPF ComboBox 默认选中无效
在WPF开发当中,我发现ComboBox的默认选中逻辑失效了,仔细查找后发现后台逻辑并没有出现问题. 测试后发现在XAML中,ComBoBox控件的SelectedValue属性需要写在ItemSou ...
- [WPF]ComboBox.Items为空时,点击不显示下拉列表
ComboBox.Items为空时,点击后会显示空下拉列表: ComboBox点击显示下拉列表,大概原理为: ComboBox存在ToggleButton控件,默认ToggleButton.IsChe ...
- WPF Combobox样式
<ControlTemplate x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}&qu ...
- WPF combobox
先写一个数据类Grade.cs using System; using System.Collections.Generic; using System.Linq; using System.Text ...
- wpf comboBox取值问题
这是获取值后台代码 private void button1_Click(object sender, RoutedEventArgs e) { combBox = ...
- WPF ComboBox Binding
public ConnectionViewModel { private readonly CollectionView _phonebookEntries; private string _phon ...
随机推荐
- LibreOJ #524. 「LibreOJ β Round #4」游戏
二次联通门 : LibreOJ #524. 「LibreOJ β Round #4」游戏 /* LibreOJ #524. 「LibreOJ β Round #4」游戏 找找规律就会发现.. 当有X的 ...
- 2019暑期金华集训 Day5 树上数据结构
自闭集训 Day5 树上数据结构 前置知识 点分治 边分治 树链剖分 LCT Top Tree LCT时间复杂度 线段树每次查询是严格\(\log n\)的,然而splay维护连续段的时候,如果每次查 ...
- opendir,readdir,closedir
结构体dirent: struct dirent { ino_t d_ino; //inode number off_t d_off; //offset to the next diret unsi ...
- BAT 批量执行SQL脚本
需要在BAT的sqlcmd中设置数据库连接信息. https://files.cnblogs.com/files/gguozhenqian/BAT%E6%89%A7%E8%A1%8CSQL%E8%84 ...
- 【Python 代码】3D TIF 拆成若干张tif (ISBI细胞数据集)
from libtiff import * imgdir = TIFF3D.open("train-labels.tif") imgarr = imgdir.read_image( ...
- JVM相关文章和GC原理算法
参考推荐: Java内存模型及GC原理 一个优秀的Java程序员必须了解的GC机制 Android 智能指针原理(推荐) Java虚拟机规范 Java虚拟机参数 Java内存模型 Java系列教程(推 ...
- RabbitMQ Management API调用
RabbitMQ在运行时,偶尔会有一些死信,即消息未正常消费,造成消息积压在消息队列中, 一直卡住,重复循环消费原来的消息队列 那么就需要有一种机制,来查看RabbitMQ是否有消息未正常消费,从而让 ...
- vue 遇到防盗链 img显示不出来
在index.html中添加: <meta name="referrer" content="no-referrer">
- 解决 screen 连接不上,提示“There is no screen to be resumed matching 18352.” 的问题
当你挂起screen,下次想重新连上screen时,有时会出现screen session的状态为Attached但是却连不上的情况,比如我想重新进入session id 为18352的screen, ...
- php中函数 isset(), empty(), is_null() 的区别
NULL:当你在你的脚本中写下这样一行代码 $myvariable; //此处你想定义一个变量,但未赋值.会有Notice: Undefined variable echo $myvariable + ...