Data Binding(2)
时间:2010-12-14 来源:小楼①夜听春雨
public class Employee : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private string _name; public String Name { set { _name = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Name")); } } get { return _name; } } public DateTime BirthDay { set; get; } public Byte Gender { set; get; } public override string ToString() { return String.Format("Name:{0}, Birth of Day :{1}, Gender:{2}",Name, BirthDay.ToLongDateString(), Gender == 1? "Male":"Female"); } }
2,将前台页面的ComboBox的 ItemsSource 属性引用到这个集合上.
public partial class MainPage : UserControl { public List<Employee> EmployeeList = new List<Employee>(); public MainPage() { InitializeComponent(); EmployeeList.Add(new Employee() { BirthDay = new DateTime(1984, 06, 28), Gender = 1, Name = "Mo.Li" }); EmployeeList.Add(new Employee() { BirthDay = new DateTime(1985, 06, 06), Gender = 1, Name = "Jerry" }); EmployeeList.Add(new Employee() { BirthDay = new DateTime(1985, 06, 06), Gender = 1, Name = "Moon.Li" }); this.myComboBox.ItemsSource = EmployeeList; } private void Button_Click(object sender, RoutedEventArgs e) { foreach(Employee p in EmployeeList) { p.Name += "_Update"; } } }
3,前台写一个ComboBox控件 名字要是myComboBox.
<Grid x:Name="LayoutRoot" Background="White"> <ComboBox Name="myComboBox" Width="350" Height="35" Background="Red"> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel Width="350" Height="35" Background="Yellow" Orientation="Horizontal"> <TextBlock Text="{Binding Name, Mode=TwoWay}" Foreground="Green" FontFamily="Georgia"></TextBlock> <TextBlock Text="{Binding BirthDay}" Foreground="Green" FontFamily="Georgia"></TextBlock> <TextBlock Text="{Binding Gender}" Foreground="Green" FontFamily="Georgia"></TextBlock> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> <Button Margin="26,98,304,172" Content="Update" Height="30" Width="70" FontSize="14" Foreground="LightBlue" Click="Button_Click"></Button> </Grid>
这就够了.
相关阅读 更多 +