【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的容量大小,其行为类似于循环 ...
随机推荐
- nginx报404的可能错误
1.转移地址写错,如root E:software_setup\ftpfile,而你实际的目录地址(比如test.jpg)在ftpfile文件夹里面的img文件夹下,如果直接找ftpfile\test ...
- 安装聊天软件telegram-cli
Telegram是一款加密通信的聊天软件,可以在linux,windows,android,chrome等运行.官方网址:https://telegram.org/ 它是有桌面版的,但作为一个linu ...
- 泛型数组 + 记录类型 + Json 之间的转换
unit Unit3; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- javascript 获取焦点和失去焦点事件
利用传参的方式提高方法的复用性 这里涉及到JavaScript的字符串拼接操作 </tr> <<tr height="40px"> <td> ...
- 分享Kali Linux 2017.1镜像
分享Kali Linux 2017.1镜像 Kali Linux官方于4月24日发布Kali Linux 2017.1版本.该版本仍然采用滚动更新方式,所以软件源为kali-rolling.至现在分 ...
- 【IntelliJ IDEA】升级之后又要激活的解决方法
用了几个月的idea,在它升级之后,又不听话了.启动时候需要重新激活. 解决方法: 1.找到C:\Windows\System32\drivers\etc\下的hosts文件 在文件中添加如下: 0. ...
- lua 的一些常用概念
1 a={} //定义了一个table a a[10000]=1 //这里的table中只有一个元素,10000,而不是有10000个元素 2 x=math.pi //定义了x等于π print( ...
- php根据日期时间生成随机编码
订单.申请单之类的需要一个编码,通过下面方法可得 代码如下: $PNUM="PG" . date("YmdHis").rand(1000,9999);
- 13.【nuxt起步】-部署到正式环境
已经购买centos服务器,并安装了nodejs环境 Secure CRT链接 Cd / Cd /var/www Mkdir test.abc.cn 用ftp 除了node_modules,其他都上传 ...
- SSD配置
SSD: Single Shot MultiBox Detector - 运行“ make -j32”时出错: nvcc warning : The 'compute_20', 'sm_20', an ...