问题场景

我有一个对象,里面有一个属性叫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. JAVA中如何创建一个二维数组,然后给二维数组赋值!

    普通的赋值是:int[][] i = {{1,2,3},{2,3,4},{1,3,4}}; 如果是其他情况可以这样:比如: import java.util.* public class TT(){ ...

  2. Codeforces 898 B.Proper Nutrition

    B. Proper Nutrition   time limit per test 1 second memory limit per test 256 megabytes input standar ...

  3. Break Number --AtCoder

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

  4. Linux基础学习3

    鸟哥私房菜第十一章   read 用来读取键盘输入的变量,常被用在shell script的交互当中 [root@www ~]# read [-pt] variable 选项不参数: -p :后面可以 ...

  5. Zend Studio 9.0.2破解文件和注册码下载

    Zend Studio是Zend Technologies开发的PHP语言集成开发环境(IDE),是公认最好的PHP开发工具.当前Zend Studio最新版本是9.0.2. Zend Studio ...

  6. linux系统中毒排查学习记录

    linux有许多的版本,主要关注redhat(centos)和ubuntu这两个主流版本 以下命令基本都需要root权限,执行命令前记得加sudo 第一步 top,ps命令查看系统资源和负载情况,查看 ...

  7. 【性能优化】——前端性能优化之DOM

    前言:本文参考学习自 RenChao Guan的博客,来源FSUX.ME,感谢原作者,本文的思维导图为自己整理 补充: 浏览器工作流程 避免重绘和回流的四种方式的具体实现

  8. UE把环境变量Path改了

    为了比较个文件,装了UE. 文件比较完了,环境变量也被改了. 改还不是写添加式的改,是写覆盖式的改. 搞得ant都起不动了,一看Path被改的那样(C:\hy\soft\ultraedit\Ultra ...

  9. GSL 1.15 and 1.16 building with Visual Studio 2010 --FROM 4fire

    http://4fire.wordpress.com/2012/03/18/gsl-1-15-building-with-visual-studio-2010/ Update 05/02/2014: ...

  10. 聚合数据Android SDK 12306火车票查询订票演示示例

    1.聚合SDK是聚合数据平台,为移动开发者提供的免费数据接口.使用前请先到聚合平台(http://www.juhe.cn/)注册,申请相关数据. 2.下载聚合数据SDK,将开发包里的juhe_sdk_ ...