WPF Combobox数据绑定 Binding
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
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的更多相关文章
- WPF combobox数据绑定和数据获取
本文章仅为个人学习,如有错误之处请指正. 搭建WPF界面的时候,想用combobox构建一个下拉菜单,搜索的时候看到大多数都是大段代码,逻辑顺序不是很详细,摸索了大概,记录一下方便来者. 拖入comb ...
- WPF数据绑定Binding(二)
WPF数据绑定Binding(二) 1.UI控件直接的数据绑定 UI对象间的绑定,也是最基本的形式,通常是将源对象Source的某个属性值绑定 (拷贝) 到目标对象Destination的某个属性上. ...
- WPF ComboBox Binding
public ConnectionViewModel { private readonly CollectionView _phonebookEntries; private string _phon ...
- wpf Content数据绑定StringFormat起作用的原理和解决
原文:wpf Content数据绑定StringFormat起作用的原理和解决 <Window x:Class="WpfOne.Bind.Bind6" xmlns=" ...
- WPF ComboBox 默认选中无效
在WPF开发当中,我发现ComboBox的默认选中逻辑失效了,仔细查找后发现后台逻辑并没有出现问题. 测试后发现在XAML中,ComBoBox控件的SelectedValue属性需要写在ItemSou ...
- WPF:数据绑定总结(1) https://segmentfault.com/a/1190000012981745
WPF:数据绑定总结(1) visual-studio c# 1.3k 次阅读 · 读完需要 16 分钟 0 一.概念:什么是数据绑定? WPF中的数据绑定:是在应用程序 UI 与业务逻辑之间建立 ...
- C# WinForm 中ComboBox数据绑定的问题 (转)
来自:http://blog.sina.com.cn/s/blog_5fb9e26301013wga.html C# WinForm 中ComboBox数据绑定的问题 怎样让WinForm中的Comb ...
- 【转】WPF中的Binding技巧(二)
WPF中的Binding技巧(二) 接上篇, 我们来看一看Elementname,Source,RelativeSource 三种绑定的方式 1.ElementName顾名思义就是根据Ui元素 ...
- combobox数据绑定
jquery easyui datagrid 可编辑行 combobox数据绑定问题 将带有参数的url地址赋值给变量,然后将变量赋值给url <script type="text/j ...
随机推荐
- StringBuild使用与原理
StringBuild的使用: 1.创建: StringBuilder sb=new StringBuilder(); StringBuilder sb=new StringBuilder(200); ...
- Java课程2019年3月开学测试
一.登录界面 模板的验证方式已经写在了function里面,我们只需要在提交的过程中进行验证. 我们这里需要注意到的是在login文件夹中,有一个randcode的验证码生成文件,打开代码我们可以看到 ...
- vscode + electron 提示:无法连接到legacy请采用inspector解决办法
首先,你的程序是可以直接运行的,在命令行中可以运行,只是在vsCode中,运行一段时间就被这个提示弹出. 解决方法: 先在launch.json 中加上"protocol":&qu ...
- mysql字符集校对
常用的两种 utf8_general_ci 按照普通的字母顺序,而且不区分大小写(比如:a B c D)utf8_bin 按照二进制排序(比如:A排在a前面,B D a c) ci是 case ins ...
- 六、web应用与Tomcat
软件系统体系结构 1 常见软件系统体系结构B/S.C/S 1.1 C/S l C/S结构即客户端/服务器(Client/Server),例如QQ: l 需要编写服务器端程序,以及客户端程序,例如我们安 ...
- 数据结构-堆 Java实现
数据结构-堆 Java实现. 实现堆自动增长 /** * 数据结构-堆. 自动增长 * */ public class Heap<T extends Comparable> { priva ...
- thinkphp 区间查询 查符合某个字段的数据 但是n个条件 用and or 配合
function get_arbeit_yuexin($screen){ $data = get_money_data_s($screen,2); dump($data['url_id']);//$d ...
- HDU 6311 Cover (无向图最小路径覆盖)
HDU 6311 Cover (无向图最小路径覆盖) Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- 栈->栈的基本定义
定义: 栈是限定仅在表尾进行插入或删除操作的线性表.因此,对栈来说,表尾端有特殊含义,称为栈顶,相应地,表头端称为栈底.不含元素的空表成为空栈. 示意图: 顺序栈的表示和实现 可以在初始化时分配一块连 ...
- scrapy_redis实现爬虫
1.scrapy_redis的流程 在scrapy_redis中,所有的带抓取的对象和去重的指纹都存在所有的服务器公用的redis中 所有的服务器公用一个redis中的request对象 所有的req ...