Combobox绑定的一段代码
2010-10-12 17:55
225 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Data;
namespace Play
{
public partial class Page : UserControl
{
people p = new people { cityid = 2, name = "Jim" };
public Page()
{
InitializeComponent();
ObservableCollection<city> citys = new ObservableCollection<city>();
citys.Add(new city { id = 1, name = "北京" });
citys.Add(new city { id = 2, name = "福建" });
citys.Add(new city { id = 3, name = "山西" });
citys.Add(new city { id = 4, name = "广东" });
this.comboBox1.ItemsSource = citys;
this.comboBox1.DisplayMemberPath = "name";
this.comboBox1.UpdateLayout();
this.comboBox1.SelectedIndex = -1;
Binding bind = new Binding("cityid");
bind.Mode = BindingMode.TwoWay;
bind.Converter = new CityidConverter(citys);
this.comboBox1.SetBinding(ComboBox.SelectedItemProperty, bind);
this.comboBox1.DataContext = p;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
this.textBox1.Text = p.ToString();
}
}
public class city : INotifyPropertyChanged
{
int _id;
string _name;
public int id { get{return _id;}
set
{
if (value != _id)
{
_id = value;
RaisePropertyChanged("id");
}
}
}
public string name { get { return _name; }
set
{
if (value != _name)
{
_name = value;
RaisePropertyChanged("name");
}
}
}
protected void RaisePropertyChanged(string propertyName)
{
if ((PropertyChanged != null))
{
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class people : INotifyPropertyChanged
{
string _name;
int _cityid;
public string name
{
get { return _name; }
set
{
if (value != _name)
{
_name = value;
RaisePropertyChanged("name");
}
}
}
public int cityid
{
get { return _cityid; }
set
{
if (value != _cityid)
{
_cityid = value;
RaisePropertyChanged("cityid");
}
}
}
public city city { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
if ((PropertyChanged != null))
{
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
public override string ToString()
{
return "cityid=" + cityid + " " + "name=" + name;
}
}
public class CityidConverter : IValueConverter
{
ObservableCollection<city> Citys;
public CityidConverter(ObservableCollection<city> citys)
{
Citys = citys;
}
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int cityid = (int)value;
//LINQ貌似很方便
return Citys.Where(c => cityid == c.id).First();
//var list = from city in Citys
// where city.id == cityid
// select city;
//foreach (var city in list)
//{
// return city;
//}
//foreach (var city in Citys)
//{
// if (city.id == cityid)
// {
// return city;
// }
//}
//return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
city c = value as city;
return c.id;
}
#endregion
}
}
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Data;
namespace Play
{
public partial class Page : UserControl
{
people p = new people { cityid = 2, name = "Jim" };
public Page()
{
InitializeComponent();
ObservableCollection<city> citys = new ObservableCollection<city>();
citys.Add(new city { id = 1, name = "北京" });
citys.Add(new city { id = 2, name = "福建" });
citys.Add(new city { id = 3, name = "山西" });
citys.Add(new city { id = 4, name = "广东" });
this.comboBox1.ItemsSource = citys;
this.comboBox1.DisplayMemberPath = "name";
this.comboBox1.UpdateLayout();
this.comboBox1.SelectedIndex = -1;
Binding bind = new Binding("cityid");
bind.Mode = BindingMode.TwoWay;
bind.Converter = new CityidConverter(citys);
this.comboBox1.SetBinding(ComboBox.SelectedItemProperty, bind);
this.comboBox1.DataContext = p;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
this.textBox1.Text = p.ToString();
}
}
public class city : INotifyPropertyChanged
{
int _id;
string _name;
public int id { get{return _id;}
set
{
if (value != _id)
{
_id = value;
RaisePropertyChanged("id");
}
}
}
public string name { get { return _name; }
set
{
if (value != _name)
{
_name = value;
RaisePropertyChanged("name");
}
}
}
protected void RaisePropertyChanged(string propertyName)
{
if ((PropertyChanged != null))
{
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class people : INotifyPropertyChanged
{
string _name;
int _cityid;
public string name
{
get { return _name; }
set
{
if (value != _name)
{
_name = value;
RaisePropertyChanged("name");
}
}
}
public int cityid
{
get { return _cityid; }
set
{
if (value != _cityid)
{
_cityid = value;
RaisePropertyChanged("cityid");
}
}
}
public city city { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
if ((PropertyChanged != null))
{
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
public override string ToString()
{
return "cityid=" + cityid + " " + "name=" + name;
}
}
public class CityidConverter : IValueConverter
{
ObservableCollection<city> Citys;
public CityidConverter(ObservableCollection<city> citys)
{
Citys = citys;
}
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int cityid = (int)value;
//LINQ貌似很方便
return Citys.Where(c => cityid == c.id).First();
//var list = from city in Citys
// where city.id == cityid
// select city;
//foreach (var city in list)
//{
// return city;
//}
//foreach (var city in Citys)
//{
// if (city.id == cityid)
// {
// return city;
// }
//}
//return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
city c = value as city;
return c.id;
}
#endregion
}
}
相关文章推荐
- winform快速开发平台之代码抽象->快速绑定ComboBox数据控件
- 从一段.html代码说起谈谈AngularJs中的双向数据绑定
- winform c#绑定combobox下拉框 年度代码。
- 关于GridView手动绑定的一段代码,一切尽在不言中
- 一段简单的ASP.NET控件的绑定代码,供于初学者参考
- 一段Spring代码引起的调用绑定总结
- C# 程序中如何向(DEV)ComboBoxEdit控件中添加下拉列表的值不使用数据绑定,就是使用代码添加
- 一段Spring代码引起的调用绑定总结
- 一段代码探究绑定服务流程
- CCocos2Dx 一段遍历子节点的代码
- 按比例微缩图片的一段小小的JS代码
- jQuery 绑定 ComponentOne Wijmo Combobox 数据源
- 有关【学过该课程的同学还学过】的一段代码
- C#实现的多列数据绑定组合框控件MultiColumnComboBoxEx
- WinForm 2.0 应用一 无代码实现详细信息绑定
- 一段C++代码,可能颠覆你已有的概念
- 一段很有意思的代码
- 一段matlab读取视频并写图像的代码
- 分享一段自己写的Linux 驱动调试寄存器的代码
- 从一段代码看Google的远大野心