combobox数据绑定List链表集合区分显示值与选择的值

整体效果:

根据combobox选择情况分别打印选取值与显示值

代码:

Windows窗体:

 <Window x:Class="ComboxBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ComBox绑定" Height="192.857" Width="385" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded">
<Grid>
<ComboBox Name="comBox1" HorizontalAlignment="Left" Margin="74,10,0,0" Width="209" Height="22" VerticalAlignment="Top"/>
<TextBlock Name="txtSelectedValue" Width="200" Text="{Binding ElementName=comBox1, Path=SelectedValue}" HorizontalAlignment="Left" Margin="115,58,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Background="#FFE7FBFA"/>
<TextBlock Name="txtSelectedText" Width="200" Text="{Binding ElementName=comBox1, Path=Text}" HorizontalAlignment="Left" Margin="114,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Background="#FFE7FBFA"/>
<Label Content="selectedValue" HorizontalAlignment="Left" Margin="2,58,0,0" VerticalAlignment="Top"/>
<Label Content="selectedText" HorizontalAlignment="Left" Margin="10,86,0,0" VerticalAlignment="Top"/> </Grid>
</Window>

Xaml

窗体cs代码

 using System.Collections.Generic;
using System.Windows; namespace ComboxBinding
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void Window_Loaded(object sender, RoutedEventArgs e)
{
List<ComboxBind> lstCmbBind = new List<ComboxBind>();//用于绑定数据源 //初始化数据源
ComboxBind cbb = new ComboxBind("显示值1", "选取值1");
lstCmbBind.Add(cbb);
cbb = new ComboxBind("显示值2", "选取值2");
lstCmbBind.Add(cbb);
cbb = new ComboxBind("显示值3", "选取值3");
lstCmbBind.Add(cbb); this.comBox1.ItemsSource = lstCmbBind;
comBox1.DisplayMemberPath = "CmbText";//类ComboxBind中的属性
comBox1.SelectedValuePath = "CmbValue";//类ComboxBind中的属性
}
}
}

用于绑定combobox的类

 namespace ComboxBinding
{
/// <summary>
/// 用于Combox数据绑定
/// </summary>
class ComboxBind
{
//构造函数
public ComboxBind(string _cmbText, string _cmbValue)
{
this.cmbText = _cmbText;
this.cmbValue = _cmbValue;
} //用于显示值
private string cmbText;
public string CmbText
{
get { return cmbText; }
set { cmbText = value; }
} //用于实际选取的值
private string cmbValue;
public string CmbValue
{
get { return cmbValue; }
set { cmbValue = value; }
}
}
}

C#

WPF Combobox数据绑定 Binding的更多相关文章

  1. WPF combobox数据绑定和数据获取

    本文章仅为个人学习,如有错误之处请指正. 搭建WPF界面的时候,想用combobox构建一个下拉菜单,搜索的时候看到大多数都是大段代码,逻辑顺序不是很详细,摸索了大概,记录一下方便来者. 拖入comb ...

  2. WPF数据绑定Binding(二)

    WPF数据绑定Binding(二) 1.UI控件直接的数据绑定 UI对象间的绑定,也是最基本的形式,通常是将源对象Source的某个属性值绑定 (拷贝) 到目标对象Destination的某个属性上. ...

  3. WPF ComboBox Binding

    public ConnectionViewModel { private readonly CollectionView _phonebookEntries; private string _phon ...

  4. wpf Content数据绑定StringFormat起作用的原理和解决

    原文:wpf Content数据绑定StringFormat起作用的原理和解决 <Window x:Class="WpfOne.Bind.Bind6" xmlns=" ...

  5. WPF ComboBox 默认选中无效

    在WPF开发当中,我发现ComboBox的默认选中逻辑失效了,仔细查找后发现后台逻辑并没有出现问题. 测试后发现在XAML中,ComBoBox控件的SelectedValue属性需要写在ItemSou ...

  6. WPF:数据绑定总结(1) https://segmentfault.com/a/1190000012981745

    WPF:数据绑定总结(1) visual-studio c# 1.3k 次阅读  ·  读完需要 16 分钟 0 一.概念:什么是数据绑定? WPF中的数据绑定:是在应用程序 UI 与业务逻辑之间建立 ...

  7. C# WinForm 中ComboBox数据绑定的问题 (转)

    来自:http://blog.sina.com.cn/s/blog_5fb9e26301013wga.html C# WinForm 中ComboBox数据绑定的问题 怎样让WinForm中的Comb ...

  8. 【转】WPF中的Binding技巧(二)

    WPF中的Binding技巧(二)     接上篇, 我们来看一看Elementname,Source,RelativeSource 三种绑定的方式 1.ElementName顾名思义就是根据Ui元素 ...

  9. combobox数据绑定

    jquery easyui datagrid 可编辑行 combobox数据绑定问题 将带有参数的url地址赋值给变量,然后将变量赋值给url <script type="text/j ...

随机推荐

  1. Yarn任务提交流程(源码分析)

    关键词:yarn rm mapreduce 提交 Based on Hadoop 2.7.1 JobSubmitter addMRFrameworkToDistributedCache(Configu ...

  2. 8. 环境变量_数据库_mongoose的基本使用_模型对象的CRUD

    1. 环境变量 系统环境(cmd)使用的变量/命令 能够让我们在cmd环境下运行指定的程序 用户环境变量 path(我们一般设置这个) 系统环境变量 path 过程: 当我们在cmd中输入一个指令 先 ...

  3. Go语言基础之切片

    Go语言基础之切片 本文主要介绍Go语言中切片(slice)及它的基本使用. 引子 因为数组的长度是固定的并且数组长度属于类型的一部分,所以数组有很多的局限性. 例如: func arraySum(x ...

  4. Prometheus 和 Grafana 安装部署

    Prometheus 是一套开源的系统监控报警框架.Prometheus 作为生态圈 Cloud Native Computing Foundation(简称:CNCF)中的重要一员,其活跃度仅次于 ...

  5. Python读取xlsx翻译文案

    首先安装Python,然后安装模块 //查找模块(非必须) pip search xlrd //安装模块 pip install xlrd 由于输出要是utf-8所以需要设置默认环境为utf-8 # ...

  6. C# DataTable 转 实体类

    C# 中查询结果DataTable转实体类: 比如:List<RtmInterview> rtmList = GetDataById( id); public List<RtmInt ...

  7. 第五周ip通信基础回顾

    广播请求,单播响应,ARP IPV4,IP地址32位二进制代码分为8个位一组 路由器每一个接口都是一个网段 ,网段与网段区分看网络地址 同一段链路是同网段 直接广播:主机号全为1 受限广播:全为1 特 ...

  8. 内置函数-max、min、round、sorted、ord、chr、any、all、dir、eval、exec、map、filter、reduce

    http://www.nnzhp.cn/archives/152 1.max,min,round print(max([3,4.563,3,6,2.5])) #取最大值,可循环参数即可,int类型的, ...

  9. linux自定义开机自启多个服务的脚本

    linux服务器重启后,每次要启动redis.ftp.tomcat等应用总是很麻烦,于是写了一个自定义脚本,在开机或重启的时候,自动启动多个服务.应用. 很简单,写脚本.设置开机启动.   第一步.准 ...

  10. CSS盒子模型 box-sizing 用法

    盒子模型  box-sizing 属性 语法:box-sizing :content-box || border-box || inherit 属性值: content-box 为(w3c标准盒子模型 ...