惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
量子位
T
Tailwind CSS Blog
T
Threatpost
The GitHub Blog
The GitHub Blog
AWS News Blog
AWS News Blog
云风的 BLOG
云风的 BLOG
K
Kaspersky official blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LangChain Blog
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
S
Secure Thoughts
The Last Watchdog
The Last Watchdog
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
T
Troy Hunt's Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
T
Tor Project blog
T
The Blog of Author Tim Ferriss
I
Intezer
P
Privacy & Cybersecurity Law Blog
美团技术团队
N
Netflix TechBlog - Medium
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
Google Developers Blog
Attack and Defense Labs
Attack and Defense Labs
T
Tenable Blog
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
A
About on SuperTechFans
Last Week in AI
Last Week in AI

博客园 - *小小黄*

List<T>的Sort,Find,Exists等的使用(摘抄) JQuery常用代码1 使用Telerik控件时出现Failed to create designer 的解决方法 GridView 笔记 - *小小黄* - 博客园 Telerik Rad 笔记 一 Silverlight学习笔记三:如何自定义DataGrid的Header Silverlight学习笔记二(续) Silverlight学习笔记二:DataGrid 服务器端分页、排序的实现 Silverlight学习笔记一:DataGrid如何处理鼠标的滚轮事件 SQL2005快照 在ASP.NET中使用HTTP压缩 Web注册表单设计样式的研究(下) Web注册表单设计样式的研究(上) 禁用aspx页面的客户端缓存 DataGrid中没有数据时显示表头(转) 在 Windows Vista 上安裝 Reporting Services IIS7中访问Access数据库报错的解决方案 解决“无法删除文件:无法读源文件或磁盘” AjaxControlToolkit中的CalendarExtender被遮挡的解决方法
Silverlight学习笔记四:如何通过自定义ComboBox实现SelectedValue
*小小黄* · 2009-01-06 · via 博客园 - *小小黄*

一、发现问题。
继续学习SL,这次用到了ComboBox,自带的ComboBox可以通过ItemSource和DisplayMemberPath来实现Items的数据绑定,DisplayMemberPath类似于Asp.net控件中的DataTextField,但是奇怪的是,ComboBox并没有提供类似与DataValueField这样的属性,也就是说,在使用SelectedItem的使用,我们无法像过去一样,直接赋一个值就可以让它选择对应的Item,现在的SelectedItem必须是ItemSource里面的一个对象,这样使用起来会非常的麻烦,比如说,我们让ComboBox绑定到Suppliers集合,DisplayMemberPath为CompanyName,这个时候,如果我们想通过SupplierID来指定ComboBox的SelectedItem时,就必须到ItemSource中,按照SupplierID来找到对应的Supplier对象,再把这个Supplier赋给SelectedItem才行。
所以这里又要骂微软了,真的不知道他们是怎么想的,学习SL这段时间来,感觉他们在做SL的时候,想的太理想化,像个科研产品,不适合商用。很多常用的功能都给他们改的面目全非,把简单问题复杂化。

二、解决问题。
要解决这个问题,我们需要自己动手来写一个新的ComboBox,并给它添加上SelectedValuePath和SelectedValue这两个属性。
这里参考了Jones的SilverLight ComboBox,这篇文章,原文在这里http://www.engineserver.com/silverlightcombobox/
代码基本上和他的一样,但是修改了他代码中的部分问题,使其更加像Asp.net中的DropDownList。

我们新建了一个类,让它继承ComboBox,并且添加了SelectedValuePathProperty和SelectedValueProperty。这样,我们就可以通过Binding来和数据库进行绑定了,我在用Jones的代码时,界面出现之后,ComboBox并不会按照SelectedValuePath来显示指定的数据,但是在手工选择ComboBox里面的Item只有,它就能很好的工作了,这个很奇怪的问题花了我不少时间,结果发现,在SetBinding的时候,虽然我们让其Bind到SelectedValueProperty上,但是系统并没有通过SelectedValue的Set来赋值,所以SelectedValue的Set的代码并不会执行,这就导致初始化的时候,ComboBox并不按照Bind的显示数据。
后来,我在SelectedValuePropertyChanged中,对SelectedValue进行了赋值,使其可以正确工作。

ok,基本上就这么多了,其实整个功能很简单。下面看代码:
自定义ComboBox的代码:

ComboBoxClassic

调用的方法:
我们假设编写一个修改数据的窗体,传入ProductID后,在界面上显示多个TextBox和ComboBox,ComboBox中显示Suppliers的信息,并和Product中的SupplierID绑定。

先通WCF获取Suppliers数据,并绑定到ItemSource上。同时设置了DisplayMemberPath和SelectedValuePath属性。

        void client_getAllSuppliersCompleted(object sender, getAllSuppliersCompletedEventArgs e)
        {
            cb.ItemsSource 
= e.Result;
            cb.DisplayMemberPath 
= "CompanyName";
            cb.SelectedValuePath 
= "SupplierID";
        }

使用时,先获取Product信息,再绑定

void client_getProductCompleted(object sender, getProductCompletedEventArgs e)
        {
            products 
= e.Result;
            
//设置ComboBox所在的Grid的DataContext。
            content.DataContext = products;
            
//设置Bind信息,让其和Product的SupplierID绑定。
            
//使用TwoWay,这样ComboBox在SelectedItem变动之后,会自动更新Product的SupplierID。
            System.Windows.Data.Binding bind = new System.Windows.Data.Binding("SupplierID");
            bind.Mode 
= System.Windows.Data.BindingMode.TwoWay;
            cb.SetBinding(ComboBoxClassic.SelectedValueProperty, bind);
        }