问题场景

我有一个对象,里面有一个属性叫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. Matcher类详解

    java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包. 它包括两个类:Pattern和Matcher . Pattern: 一个Pattern是一个正则表达式经编 ...

  2. weblogic内存快速配置

    # IF USER_MEM_ARGS the environment variable is set, use it to override ALL MEM_ARGS values USER_MEM_ ...

  3. CentOS 7 配置阿里云yum源

    Test at Red Hat Enterprise Linux Server release 7.5 (Maipo) File localtion /etc/yum.repos.d/epel.rep ...

  4. bzoj 4465: [Jsoi2013]游戏中的学问

    4465: [Jsoi2013]游戏中的学问 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 121  Solved: 59[Submit][Statu ...

  5. 笔记-迎难而上之Java基础进阶8

    函数式接口 函数式接口:有且只有一个抽象方法的接口,(可以包含其他默认,静态,私有方法) //函数式接口的使用,创建一个函数式接口 public interface FunctionalInterfa ...

  6. Java Servlet Filter

    做web开发的人对于Filter应该不会陌生,一直在很简单的使用,但是一直没有系统的总结一下,随着年纪的慢慢长大,喜欢总结一些事情,下面说说我对Filter的理解,官方给出的Filter的定义是在请求 ...

  7. 设置ListView的item不能点击

    写了一个ListView结合volley的demo ListView只是用来展示数据,所以不需要点击效果. 网上搜索了下: 可以禁用ListView ListView.setEnabled(false ...

  8. OBS插件开发以及OBS插件的选择(obs直播插件)研究思路

    obs版本的选择: 工作室版,优化了很多东西,缺点是不能用插件,在部分机型不稳定,因为更新的很频繁.不过这个插件不能用的说法还是停留在早起,截至到今天已经完美支持,所以在不久的将来会越来越好,如果是开 ...

  9. 【ActiveMQ】管理界面查看消息详情,报错/WEB-INF/tags/form/forEachMapEntry.tag PWC6199: Generated servlet error: The type java.util.Map$Entry cannot be resolved. It is indirectly referenced from required .class files

    ActiveMQ版本:5.12 JDK版本:1.8 ===================== 使用ActiveMQ过程中,在管理界面查看消息详情,发现报错: 查看日志信息,报错如下: 2017-11 ...

  10. nginx phase handler的原理和选择

    nginx phase handler的原理和选择 PHASE HANDLER的种类 nginx在接收并解析完请求行.请求头之后.就会依次调用各个phase handler. phase handle ...