ComboBox 的绑定

 

在WPF(Windows Presentation Foundation)中,ComboBox 绑定是常见的操作,可绑定到不同类型的数据,以下是几种常见的绑定方法。 ### 绑定到简单集合 如果要将 ComboBox 绑定到一个简单的字符串集合,可以按照以下步骤操作。 ```xml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <ComboBox x:Name="comboBox" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" /> </Grid> </Window> ``` ```csharp using System.Collections.Generic; using System.ComponentModel; using System.Windows; namespace WpfApp1 { public partial class MainWindow : Window, INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private List<string> _items; public List<string> Items { get { return _items; } set { _items = value; OnPropertyChanged(nameof(Items)); } } private string _selectedItem; public string SelectedItem { get { return _selectedItem; } set { _selectedItem = value; OnPropertyChanged(nameof(SelectedItem)); } } public MainWindow() { InitializeComponent(); DataContext = this; Items = new List<string> { "Item 1", "Item 2", "Item 3" }; } protected void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } } ``` 在上述代码中,首先在 XAML 里定义了一个 ComboBox,并将 `ItemsSource` 属性绑定到 `Items` 集合,`SelectedItem` 属性绑定到 `SelectedItem` 属性。在 C# 代码里实现了 `INotifyPropertyChanged` 接口,用于通知 UI 当属性值发生变化。 ### 绑定到对象集合 若要将 ComboBox 绑定到一个对象集合,并且显示对象的某个属性,可以使用 `DisplayMemberPath` 属性。 ```xml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <ComboBox x:Name="comboBox" ItemsSource="{Binding People}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedPerson}" /> </Grid> </Window> ``` ```csharp using System.Collections.Generic; using System.ComponentModel; using System.Windows; namespace WpfApp1 { public class Person { public string Name { get; set; } public int Age { get; set; } } public partial class MainWindow : Window, INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private List<Person> _people; public List<Person> People { get { return _people; } set { _people = value; OnPropertyChanged(nameof(People)); } } private Person _selectedPerson; public Person SelectedPerson { get { return _selectedPerson; } set { _selectedPerson = value; OnPropertyChanged(nameof(SelectedPerson)); } } public MainWindow() { InitializeComponent(); DataContext = this; People = new List<Person> { new Person { Name = "John", Age = 25 }, new Person { Name = "Jane", Age = 30 }, new Person { Name = "Bob", Age = 35 } }; } protected void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } } ``` 这里定义了一个 `Person` 类,ComboBox 的 `DisplayMemberPath` 属性设置为 `Name`,这样 ComboBox 会显示 `Person` 对象的 `Name` 属性。 ### 相关资料 - MSDN 文档:微软官方的 WPF 文档对 ComboBox 绑定有详细的介绍和示例。 - 《Pro WPF in C# 2012 and .NET 4.5》:这本书全面介绍了 WPF 的各种特性,包括数据绑定
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值