一、Combox绑定

场景:定义多个Person,Person有Name和Age属性,将多个Person与Combox进行绑定,Combox中只显示Name信息,点击任意一个item,在左侧显示该条目的详细信息。

参考代码以下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data; namespace BindingDemo2
{
public partial class MainWindow : Window
{
private List<Person> personList = new List<Person>();
public MainWindow()
{
InitializeComponent();
personList.Add(new Person() {Name = "Mary",Age = 18 });
personList.Add(new Person() { Name = "Tom", Age = 15 });
personList.Add(new Person() { Name = "Jack", Age = 28 });
personList.Add(new Person() { Name = "Jim", Age = 38 });
cmbPerson.ItemsSource = personList;
}
}
public class Person : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged; if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
private int _age = 18; public int Age
{
get { return _age; }
set
{
_age = value;
OnPropertyChanged("Age");
}
} private string _name = "Mary"; public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("Name");
}
}
public Person()
{
Age = 18;
Name = "Mary";
} }
}
<Window x:Class="BindingDemo2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:BindingDemo2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Name="win">
<Grid ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel>
<Label Content="Target"/>
<Label Content="Name"/>
<Label Content="{Binding SelectedItem.Name,ElementName=cmbPerson}" Height="30" Background="AliceBlue" Name="lbName"/>
<Label Content="Age"/>
<Label Height="30" Background="AliceBlue" Name="lbAge" Content="{Binding SelectedItem.Age, ElementName=cmbPerson}"/>
</StackPanel>
<StackPanel Grid.Column="1">
<ComboBox Name="cmbPerson" ItemsSource="{Binding}" DisplayMemberPath="Name" SelectedIndex="0">
</ComboBox>
</StackPanel>
</Grid>
</Window>

运行结果如下:

我们也可以利用DataContext属性来简化前台绑定,参考代码以下:

<Window x:Class="BindingDemo2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:BindingDemo2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Name="win">
<Grid ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel DataContext="{Binding ElementName=cmbPerson, Path=SelectedItem}">
<Label Content="Target"/>
<Label Content="Name"/>
<Label Content="{Binding Name}" Height="30" Background="AliceBlue" Name="lbName"/>
<Label Content="Age"/>
<Label Height="30" Background="AliceBlue" Name="lbAge" Content="{Binding Age}"/>
</StackPanel>
<StackPanel Grid.Column="1">
<ComboBox Name="cmbPerson" ItemsSource="{Binding}" DisplayMemberPath="Name" SelectedIndex="0">
</ComboBox>
</StackPanel>
</Grid>
</Window>

最新文章

  1. Linux操作系统启动流程梳理
  2. stm32软件模拟IIC读取PX4FLOW光流传感器数据
  3. WdatePicker 设置日期第一个比第二个的日期小
  4. ssh中getHibernateTemplate()的方法
  5. 对LVS DR模式的理解
  6. 用document.getElementsByTagName()返回的真的是数组吗?
  7. wildcard
  8. javascript笔记整理(流程控制)
  9. 基于docker+reveal.js搭建一个属于自己的在线ppt网站
  10. MySQL分类表设计--根据ID删除全部子类
  11. 一些常用的api接口、
  12. Beta 冲刺day3
  13. Django rest framework源码分析(2)----权限
  14. 处理Word文档中所有修订
  15. C# GDI+编程之Graphics类
  16. Vue单页面应用
  17. Python——eventlet
  18. 关于struts.xml配置文件的说明
  19. mac下framework联编需要设置的
  20. Qt中QScrollArea类的简单使用心得

热门文章

  1. StreamAPI
  2. 全站 HTTPS 就一定安全了吗?
  3. [NOIp2017]宝藏 题解
  4. 初识Stream API + Lambda表达式
  5. 使用Visual Studio进行文件差异比较
  6. 添加数据时报错:An error occurred while updating the entries. See the inner exception for detail。
  7. Mybatis学习笔记-分页
  8. 使用 GLFW 在 OpenGL 的场景中漫游
  9. 【数据结构与算法】字符串匹配(Rabin-Karp 算法和KMP 算法)
  10. VGG Net 论文细读