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 ...
随机推荐
- learning java ATW ScrollPane
import java.awt.*; public class ScrollPaneTest { public static void main(String[] args) { var f = ne ...
- saltstack 发布 iis 站点
Saltstack 发布 iis 站点 saltstack 主服务器配置:切换到 salt 的主目录下 : 主目录示例:/home/salt 程序集放置目录: web/web1 sls 目录: web ...
- arm 环境下安装selenium+chrome
1. 升级软件 apt-get update 2. 安装pip apt-get install python3-pip 3. 安装selenium pip3 install selenium .4. ...
- Launch4j:An error occurred while starting the application.解决方案
长期使用Processing 2.X进行开发,突然有一天Processing 1.5.1打不开了,报错如下: 按[确定]后窗口消失,但是任务管理器中的“javaw.exe”并没有消失..... 试过各 ...
- 生成ROM时修改参数
在Xilinx ISE中生成ROM后,如果修改参数,可能会出现错误: “The Memory Initialization vector can contain between 1 to Write” ...
- 2019暑期金华集训 Day7 动态规划
自闭集训 Day7 动态规划 LOJ6395 首先发现这个树的形态没啥用,只需要保证度数之和是\(2n-2\)且度数大于0即可. 然后设\(dp_{i,j}\)表示前\(i\)个点用了\(j\)个度数 ...
- 《挑战30天C++入门极限》理解C++面向对象程序设计中的抽象理论
理解C++面向对象程序设计中的抽象理论 很多书在一开始就开始学习josephus问题,为了让大家前面学起来较为容易我把前面涉及到此问题的地方都故意去掉了,现在我们已经学习过了结构体和类,所以放 ...
- jquery.nicescroll.js Unable to preventDefault inside passive event listener due to target being treated as passive.
解决办法就是:https://github.com/bestjhh/Plugin 下载替换. 参考: https://github.com/bestjhh/Plugin https://blog.cs ...
- SpringBoot整合ElasticSearch:基于Jest技术
1.给pom.xml添加依赖 <!--SpringBoot默认使用SpringData ElasticSearch模块进行操作 <dependency> <groupId> ...
- Docker 常用命令,自用,持续更
1.进入容器 docker exec -it 容器id /bin/bash docker exec -it db30f533ee1b /bin/bash 2.复制文件到容器 docker cp 文件路 ...