【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的容量大小,其行为类似于循环 ...
随机推荐
- JSON Web Token的使用
定义 JSON Web Token(JWT)是一个非常轻巧的规范.这个规范允许我们使用JWT在用户和服务器之间传递安全可靠的信息. 适用场景 1.用于向Web应用传递一些非敏感信息.例如完成加好友.下 ...
- hdu 4991(树状数组+DP)
Ordered Subsequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- autofac 用法总结
autofac官网: http://autofaccn.readthedocs.io/en/latest/getting-started/index.html autofac作为一个热门ioc框架,还 ...
- python对象的复制问题
list 的拷贝问题: 1, >>> a [1, 2] >>> b=a[:] >>> b [1, 2] >>> b[0]=20 ...
- Codeforces 785D Anton and School - 2(推公式+乘法原理+组合数学)
题目链接 Anton and School - 2 对于序列中的任意一个单括号对(), 左括号左边(不含本身)有a个左括号,右括号右边(不含本身有)b个右括号. 那么答案就为 但是这样枚举左右的()的 ...
- Light oj 1095 - Arrange the Numbers (组合数学+递推)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1095 题意: 给你包含1~n的排列,初始位置1,2,3...,n,问你刚好固定 ...
- x86服务器中网络性能分析与调优 转
x86服务器中网络性能分析与调优 2017-04-05 巨枫 英特尔精英汇 [OpenStack 易经]是 EasyStack 官微在2017年新推出的技术品牌,将原创技术干货分享给您,本期我们讨论 ...
- Sort Detail Data Block Example - Oracle Forms
Example is given below to sort detail data block data (toggle asc or desc) with push buttons used as ...
- Scut游戏服务器引擎之新手入门
1. 开发语言:Scut提供C#或Python两种脚本语言开发,Python脚本的性能会比较差,建议使用编译执行的C#代码: 2. 运行平台:Scut可以Window与Linux平台上运行,Linux ...
- python pandas相关知识点(练习)
首先引入库文件,并进行数据读取 import pandas as pd import numpy as np data_Base=pd.read_csv("D:\\Exam_Test\\un ...