Combobox绑定的一段代码
时间:2010-10-12 来源:牛小浩
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
}
}