【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的容量大小,其行为类似于循环 ...
随机推荐
- vue中v-model的一点使用心得
我的data里面有个值是字典的对象: config_template: {}, 这个值会被后端返回的数据填充,填充后大概是这样的: u 'config_template': { u 'startSh ...
- docker下使用DB2
1.查询可安装的db2镜像 benjamin@docker:~$ docker images |grep -i db2 ibmcom/db2express-c latest 7aa154d9b73c ...
- 转载——C# 6.0可能的新特性及C#发展历程
据扯,C# 6.0在不远的将来就发布了,对应的IDE可能是VS 2014(.Net Framework 5.0),因为VS 2013已于2013年10月份发布了,对应的是.Net Franework ...
- Codeforces Gym100971 K.Palindromization-回文串 (IX Samara Regional Intercollegiate Programming Contest Russia, Samara, March 13)
这个题就是从字符串中删除一个字符,然后剩下的是回文串. 我写的代码虽然长得好看,但是循环里面的比较条件容易想错,太智障了... 一开始写的是计数比较,但是有的时候下标相同的也比较了,为了简单一些,直接 ...
- 51nod 1050 循环数组最大子段和【环形DP/最大子段和/正难则反】
1050 循环数组最大子段和 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 N个整数组成的循环序列a[1],a[2],a[3],…,a[n],求该 ...
- encodeURI 解码 编码
var uriStr = "http://www.baidu.com?name=张三&num=001 zs"; var uriec = encodeURI(uriStr); ...
- luogu P2744 [USACO5.3]量取牛奶Milk Measuring
题目描述 农夫约翰要量取 Q(1 <= Q <= 20,000)夸脱(夸脱,quarts,容积单位——译者注) 他的最好的牛奶,并把它装入一个大瓶子中卖出.消费者要多少,他就给多少,从不有 ...
- spring 自定义解析类
设计配置属性和JavaBean 编写XSD文件 编写NamespaceHandler和BeanDefinitionParser完成解析工作 编写spring.handlers和spring.schem ...
- Docking For WPF–AvalonDock
桌面程序的应用,不可避免的就会用到大量的布局控件,之前的一个项目也想过去做类似于Visual Studio的那种灵活的布局控件,也就是界面上的控件能够实现拖拽放置.隐藏.窗口化等一系列的操作,但由于开 ...
- 【IntelliJ Idea】idea下hibernate反向生成工具,根据数据表生成实体
idea插件很齐全,不像ecplise一样.所以直接来步骤吧: 1.选择项目,右键-->Add Frameworks Support-->勾选Hibernate-->勾选Import ...