问题场景

我有一个对象,里面有一个属性叫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:根据绑定选取、设置固定集合中的值的更多相关文章

  1. Java——删除Map集合中key-value值

    通过迭代器删除Map集合中的key-value值 Iterator<String> iter = map.keySet().iterator(); while(iter.hasNext() ...

  2. (WPF) ComboBox 之绑定

    1.  在UI(Xaml) 里面直接绑定数据. <Window x:Class="WpfTutorialSamples.ComboBox_control.ComboBoxSample& ...

  3. comboBox绑定字典Dictionary 获取value中的值

    第一种 最简洁的方法 Dictionary<string, string> list = new Dictionary<string, string> { {"thi ...

  4. wpf,记录一下颜色设置的2中方法,,,

    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color ...

  5. javascript中遍历EL表达式List集合中的值

    http://www.cnblogs.com/limeiky/p/6002900.html

  6. selenium python选取下拉框中的值

    https://stackoverflow.com/questions/47689936/unable-to-scroll-and-select-desired-year-from-calender- ...

  7. 关于MongoDB 固定集合(capped collection)的知识梳理

    一 . 什么是固定集合 MongoDB中有一种特殊类型的集合,值得我们特别留意,那就是固定集合(capped collection). 固定集合可以声明collection的容量大小,其行为类似于循环 ...

  8. 第32章:MongoDB-索引--Capped固定集合

    ①Capped集合(固定集合) Capped集合的大小固定,性能好,如果空间用完了,新的对象会覆盖旧的对象. find时默认就是插入的顺序,Capped集合会自动维护. ②语法 db.createCo ...

  9. MongoDB固定集合(capped collection)

    一 . 什么是固定集合 MongoDB中有一种特殊类型的集合,值得我们特别留意,那就是固定集合(capped collection). 固定集合可以声明collection的容量大小,其行为类似于循环 ...

随机推荐

  1. 《手把手教你学C语言》学习笔记(6)---数据类型和常量

    计算机中需要保存信息,就需要数据存储,数据的存储就需要划分数据类型.主要包括:基本数据类型.指针类型.构造类型.空类型. 基本类型:整型---主要用来表示整数,可以分为无符号和有符号:又分为基本整型. ...

  2. 【linux高级程序设计】(第七章)终端及串口编程 未完成

    一.端口设备类型 1.显示设备基本信息 cat /proc/tty/drivers 里面包括了: 当前终端:/dev/tty 前台控制台终端:/dev/console 用于创建虚拟终端的:/dev/p ...

  3. Python Challenge 第十二关

    这一关依旧只有一张图,右键源代码也没有任何注释,也用PIL处理过那张图但没任何头绪,没办法只有上网搜答案. 别人的博客里说,源代码里面图片的名字是 evil1.jpg,那肯定会有 evil2.jpg. ...

  4. 安卓全屏状态下键盘充满屏幕留不出ui控件的解决办法附edittext和键盘的属性

    1.我们先看看常用和不常用的属性值(Edittext) android:inputType参数类型说明 android:inputType="none"--输入普通字符 andro ...

  5. NOIP 2014飞扬的小鸟(DP优化)

    题目链接  飞扬的小鸟 考场的70分暴力(实际只有50分因为数组开小了……) 考场代码(数组大小已修改) #include <cstdio> #include <cstring> ...

  6. Break Number --AtCoder

    题目描述 Takahashi loves numbers divisible by 2.You are given a positive integer N. Among the integers b ...

  7. 小菜的系统框架界面设计-XiaoCai.WinformUI代码开源

    我的源码分享 曾经,看到别人漂亮的系统界面,合理的布局,可是却没有提供源码,道理很简单,就是有偿提供,实际上对于有些技巧的东西也并没有多么难,只是不懂原理,感觉到困难罢了. 而对于刚毕业的我,求知欲强 ...

  8. 巧用chrome开发者工具

    说明:截图中的Chrome版本为52,不同版本可能略有区别. 常用设置 开发时消除静态资源缓存不能立刻更新的困扰,勾选Disable cache即可 切换颜色显示格式 修改默认颜色显示格式,在Sett ...

  9. java加载类的方法1.classloader 2.class.forName()

    java加载类的方法1.classloader 2.class.forName() 加载一个类后,是在方法去创建这个类的元信息class对象,在方法区立刻创建.在方法区创建.

  10. 【GLSL教程】(九)其他说明 【转】

    http://blog.csdn.net/racehorse/article/details/6664775 法线矩阵 在很多顶点shader中都用到了gl_NormalMatrix.这里将介绍这个矩 ...