首页 > 编程语言 >How To Bind a Combobox to a Dictionary in WPF C#

How To Bind a Combobox to a Dictionary in WPF C#

时间:2022-09-19 18:44:16浏览次数:94  
标签:Dictionary C# Combobox How Bind Answer

How To Bind a Combobox to a Dictionary in WPF C#

 

回答1

Use a Dictionary<Answer,string> (no need for another class)

AnswerDisplay = new Dictionary<Answer, string>
{
    {Answer.YES, "I will do it"},
    {Answer.NO,  "I will not do it"},
    {Answer.MAYBE, "I might do it"},
};

and bind it to the ComboBox

<ComboBox ItemsSource="{Binding AnswerDisplay}" 
          DisplayMemberPath="Value"
          SelectedValuePath="Key"
          SelectedValue="{Binding SelectedAnswer}"/>

Update

If you want to use your dictionary, then change the binding to

<ComboBox ItemsSource="{Binding AnswerDisplay}" 
          DisplayMemberPath="Value.DisplayDescription"
          SelectedValuePath="Key"
          SelectedValue="{Binding SelectedAnswer}"/>

 

标签:Dictionary,C#,Combobox,How,Bind,Answer
From: https://www.cnblogs.com/chucklu/p/16708656.html

相关文章

  • ACL的应用
    掌握LinuxACL权限的全部内容,看这篇就够了!点击关注......
  • Currying
    Currying(javascript.info)Curryingisanadvancedtechniqueofworkingwithfunctions.It’susednotonlyinJavaScript,butinotherlanguagesaswell.......
  • BigDecimal除法报错Method threw ‘java.lang.ArithmeticException‘ exception.
    今天使用BigDecimal数据类型做除法的时候,遇到以下报错:Methodthrew‘java.lang.ArithmeticException’exception.Non-terminatingdecimalexpansion;noexactreprese......
  • 【Mysql】什么是MVCC
    多版本并发控制:读取数据时通过一种类似快照的方式将数据保存下来,这样读锁就和写锁不冲突了,不同事务会话看到自己特定版本的数据,使用版本链。MVCC只在READCOMMITTED和RE......
  • static
    Static首先static无规定private还是public,默认为default的形式,一般public可以跨类跨包,private只能由对象本身访问,而protected可以在本类和子类下跨包,default可以实现同一......
  • 安装centos7实验报告
    实验任务安装centos7系统 实验环境  一台centos7实验步骤 1.  创建新centos打开虚拟机  点击左上角文件的选项......
  • qt creator 使用 git
    创建仓库在github,gitee这样的平台上,创建一个仓库,注意不要初始化仓库配置项目创建项目的时候,cvs选择git工具。tool->git->remoterepo->manageremote,......
  • 逃逸分析(Escape Analysis)详解
    概念说明逃逸分析,是一种可以有效减少Java程序中同步负载和内存堆分配压力的跨函数全局数据流分析算法。通过逃逸分析,JavaHotspot编译器能够分析出一个新的对象......
  • javascript的类的继承
     先看下java的继承,java的继承是发生在类之间,他们的对象之间没有继承关系,子类的对象通过子类创建对象,同时对象中不存储函数只有数据,函数需找到类定义以后读取里面的函数,类......
  • centos7离线部署ntp服务,实现内网服务器时间(转载)
    1、简介NTP(NetworkTimeProtocol,网络时间协议)是用来使计算机时间同步的一种协议。它可以使计算机对时钟源做同步化,提供高精准度的时间校正。通过搭建ntp服务,可以为内网......