【WPF】ComboBox:根据绑定选取、设置固定集合中的值
问题场景
我有一个对象,里面有一个属性叫Limit,int类型。虽然int可取的范围很大,我想要在用户界面上限制Limit可取的值,暂且限制为5、10、15、20。
所以ComboBox绑定不是绑定常见的ItemsSource(至少初看起来不是),而是Text、SelectedItem、SelectedValue或是什么东西,先卖个关子。
另外,Limit是表示时间的,单位秒。我要求ComboBox上能显示10秒、5秒,而不是光秃秃的10和5。
解决方案
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" SizeToContent="WidthAndHeight">
<StackPanel Margin="10">
<ComboBox Name="comboBox" ItemStringFormat="{}{0}秒" SelectedValue="{Binding Limit, RelativeSource={RelativeSource AncestorType=Window}}" />
<Button Content="读" Click="Button_Click" />
</StackPanel>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
Limit = 10;
InitializeComponent();
comboBox.Items.Add(10);
comboBox.Items.Add(15);
comboBox.Items.Add(20);
comboBox.Items.Add(25);
}
public int Limit { get; set; }
private void Button_Click(object sender, RoutedEventArgs e)
{
//此处下断点来看Limit到底变了没有。
}
}
小结:使用ItemStringFormat来加上“秒”字,用SelectedValue选取值。
能不能用纯XAML完成呢?
<ComboBox Name="comboBox" ItemStringFormat="{}{0}秒" SelectedValue="{Binding Limit, RelativeSource={RelativeSource AncestorType=Window}}">
<ComboBoxItem>
<System:Int32>10</System:Int32>
</ComboBoxItem>
<ComboBoxItem>
<System:Int32>15</System:Int32>
</ComboBoxItem>
<ComboBoxItem>
<System:Int32>20</System:Int32>
</ComboBoxItem>
<ComboBoxItem>
<System:Int32>25</System:Int32>
</ComboBoxItem>
</ComboBox>
以上方案是不行的!因为ComboBox的集合已经是ComboBoxItem,而不是int了。所以还得回归ItemsSource。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" SizeToContent="WidthAndHeight">
<StackPanel Margin="10">
<StackPanel.Resources>
<x:Array x:Key="candidateValues" Type="System:Int32">
<System:Int32>10</System:Int32>
<System:Int32>15</System:Int32>
<System:Int32>20</System:Int32>
</x:Array>
</StackPanel.Resources>
<ComboBox Name="comboBox" ItemStringFormat="{}{0}秒" ItemsSource="{StaticResource candidateValues}" SelectedValue="{Binding Limit, RelativeSource={RelativeSource AncestorType=Window}}">
</ComboBox>
<Button Content="读" Click="Button_Click" />
</StackPanel>
</Window>
如上即可,记得删除MainWindow构造函数里给comboBox加元素的语句。
本文以知识共享-署名-相同方式共享方式发布
作者爱让一切都对了
【WPF】ComboBox:根据绑定选取、设置固定集合中的值的更多相关文章
- Java——删除Map集合中key-value值
通过迭代器删除Map集合中的key-value值 Iterator<String> iter = map.keySet().iterator(); while(iter.hasNext() ...
- (WPF) ComboBox 之绑定
1. 在UI(Xaml) 里面直接绑定数据. <Window x:Class="WpfTutorialSamples.ComboBox_control.ComboBoxSample& ...
- comboBox绑定字典Dictionary 获取value中的值
第一种 最简洁的方法 Dictionary<string, string> list = new Dictionary<string, string> { {"thi ...
- wpf,记录一下颜色设置的2中方法,,,
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color ...
- javascript中遍历EL表达式List集合中的值
http://www.cnblogs.com/limeiky/p/6002900.html
- selenium python选取下拉框中的值
https://stackoverflow.com/questions/47689936/unable-to-scroll-and-select-desired-year-from-calender- ...
- 关于MongoDB 固定集合(capped collection)的知识梳理
一 . 什么是固定集合 MongoDB中有一种特殊类型的集合,值得我们特别留意,那就是固定集合(capped collection). 固定集合可以声明collection的容量大小,其行为类似于循环 ...
- 第32章:MongoDB-索引--Capped固定集合
①Capped集合(固定集合) Capped集合的大小固定,性能好,如果空间用完了,新的对象会覆盖旧的对象. find时默认就是插入的顺序,Capped集合会自动维护. ②语法 db.createCo ...
- MongoDB固定集合(capped collection)
一 . 什么是固定集合 MongoDB中有一种特殊类型的集合,值得我们特别留意,那就是固定集合(capped collection). 固定集合可以声明collection的容量大小,其行为类似于循环 ...
随机推荐
- Fennec VS. Snuke --AtCoder
题目描述 Fennec and Snuke are playing a board game.On the board, there are N cells numbered 1 through N, ...
- Maven项目导入到Eclipse时Build出现the user operation is waiting for building workspace to complete的问题解决
解决办法如下: 1.选择菜单栏的[Project],然后把菜单栏中[Build Automatically]前面的对钩去掉.
- advanced-performance-troubleshooting-waits-latches-spinlocks
https://www.sqlskills.com/blogs/paul/advanced-performance-troubleshooting-waits-latches-spinlocks/
- Windows远程命令执行0day漏洞安全预警
网站安全云检测这不是腾讯公司的官方邮件. 为了保护邮箱安全,内容中的图片未被显示. 显示图片 信任此发件人的图片 一.概要 Shadow Brokers泄露多个Windows 远程漏洞利用工具 ...
- 【spring boot】在自定义拦截器中从request中获取json字符串
又这样的需求,需要在自定义的拦截器中获取request中的数据,想获取到的是JSON字符串 那需要在拦截器中写这样一个方法 public static String getOpenApiRequest ...
- Android开发初期之后怎么提升?怎么才能叫精通?方向在哪?
hi大头鬼hi Android开发专家 先mark一下,好多人我发现始终停留在两三年的水平上没有突破. 另外还有一个误区就是越底层越牛逼 第三个就是,我认识的大部分所谓的做过rom开发的对fr ...
- sublime的markdown插件
mac安装 shift+command+p调出package control面板,搜索install调查安装软件搜索面板 搜索需要安装markdown软件 我安装了下面两个:MarkdownLiveP ...
- SSH 框架搭建与开发
对于Java初学者而言,SSH框架还是比较复杂的,今天借用一个Web注册功能的案例给大家讲解下,主要是讲下开发模式与注意事项! 注册界面如下所示: 1.首先建库建表(我用的是Mysql数据库,大家可以 ...
- springMVC --配置具体与注讲解明
<?xml version="1.0" encoding="UTF-8"? > <beans xmlns="http://www.s ...
- Batch update returned unexpected row count from update [0];
Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested ...