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 ...
随机推荐
- 使用163.com邮箱发送邮件
不要直接使用登录的密码,而是用配置中的授权码做为密码
- Understanding HBase and BigTable
Hbase is a distributed data storage systems. A Bigtable is spare , distributed , persistent multidim ...
- HTML5_提供的 新功能_less 编译_
HTML5_提供的 新功能 class 操作 ele.classList(注意: 高版本的 IE 都不支持) 获取 <div id="ele" class="... ...
- Educational Codeforces Round 6
620A - Professor GukiZ's Robot 20171122 \(ans=max(\left | x2-x1 \right |,\left | y2-y1 \right |)\ ...
- linux学习:sed与awk与tr用法整理
流编辑器:sed 语法:sed [-hnV][-e<script>][-f<script文件>][文本文件] 参数: -e<script>或--expression ...
- cadence分列元件原理图库的设计
- android发送短信验证码并自动获取验证码填充文本框
android注册发送短信验证码并自动获取短信,截取数字验证码填充文本框. 一.接入短信平台 首先需要选择短信平台接入,这里使用的是榛子云短信平台(http://smsow.zhenzikj.com) ...
- raycast 一小段距离碰撞到的poly
dtNavMeshQuery::raycast(dtPolyRef startRef, const float* startPos, const float* endPos, const dtQuer ...
- C# 复选框显示多项选择
private void Form1_Load(object sender, EventArgs e) { checkedListBox1.Items.Add("语文"); che ...
- Design Principle, Design Patterns And Refactoring
https://refactoring.guru/smells/feature-envy https://stackoverflow.com/questions/1242994/effective-c ...