023.WPF combox控件数据绑定
使用 MVVM 模式来绑定 ComboBox 数据是 WPF 应用程序中一种常见的做法。下面是一个简单的示例演示如何使用 MVVM 模式实现 ComboBox 的数据绑定。1. 创建 WPF 项目首先创建一个新的 WPF 应用程序项目。2. 创建模型Model在项目中您可以创建一个简单的模型类例如chain_combox.cs虽然在这个例子中我们可以直接使用字符串但为了更好地展示 MVVM 的结构可以定义一个模型public class chain_combox { private int name; public int Name { get { return name; } set { name value; } } }3. 创建视图模型ViewModel创建一个视图模型类例如MainViewModel.cs这个类将去控制一个Chain子类方便代码管理主控制类继承ObservableObject订阅通知类public class MainViewModel : ObservableObject { public Chain Mychain { get; } new Chain();//链条数据类绑定 }4.写一个广播通知类ObservableObject一般的mvvm包都会自带有ObservableObject这个工具类,不需要自己写工具类,但我的程序是用在旧项目上的 ,无法使用到工具包,所以需要自己封装这个广播通知类工具public class ObservableObject : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } protected bool SetPropertyT(ref T storage, T value, [CallerMemberName] string propertyName null) { if (Equals(storage, value)) { return false; } storage value; OnPropertyChanged(propertyName); return true; } }5.创建子控制器类Chainusing NXOpen.CAE; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using UG_API.comboxs; namespace UG_API.Models { /// summary /// 链条数据类 /// /summary public class Chain : ObservableObject { //用ObservableCollection标签自动更新ITems private ObservableCollectionchain_combox items; public ObservableCollectionchain_combox Items { get items; set SetProperty(ref items, value); // 使用SetProperty通知更新 } //定义成员属性,成员是每一个chain_combox模型类对象 private chain_combox selectedItem; public chain_combox SelectedItem { get selectedItem; set SetProperty(ref selectedItem, value);// 使用SetProperty通知更新 } //构造类似加载预先加载 public Chain() { Items new ObservableCollectionchain_combox(); // 使用循环加载从 8 到 114 的项 for (int i 8; i 114; i) { Items.Add(new chain_combox { Name i }); } if (Items.Count 0) { SelectedItem Items[9]; // 设置默认选中项为第一个 } } } }6.界面ui绑定用主控制器的MyChain去调用子控制器的Items属性ComboBox Namecombox7 HorizontalAlignmentLeft ItemsSource{Binding Mychain.Items} SelectedItem{Binding Mychain.SelectedItem, ModeTwoWay} DisplayMemberPathName Margin276,32,0,0 VerticalAlignmentTop Width120/7.在主窗体后台绑定主控制器public partial class HomePage : Window { MainViewModel home new MainViewModel(); public HomePage() { InitializeComponent(); this.DataContext home;//数据绑定 } }运行结果:成功加载了combox的项二.为combox添加事件,获取选中项的值要为 ComboBox 控件增加事件以获取选中项的内容您可以使用SelectionChanged事件。下面是如何在 WPF 中实现这一功能的步骤。1. 更新 XAML 代码在MainWindow.xaml中您需要为 ComboBox 添加SelectionChanged事件处理程序。以下是更新后的 XAML 示例ComboBox Namecombox7 HorizontalAlignmentLeft ItemsSource{Binding Mychain.Items} SelectedItem{Binding SelectedItem, ModeTwoWay} DisplayMemberPathName SelectionChangedcombox7_SelectionChanged Margin276,32,0,0 VerticalAlignmentTop Width120/2. 添加事件处理程序在MainWindow.xaml.cs中添加ComboBox_SelectionChanged事件处理程序以获取并显示选中的项的内容/// summary /// 选中的主链轮的齿数 /// /summary /// param namesender/param /// param namee/param private void combox7_SelectionChanged(object sender, SelectionChangedEventArgs e) { // 获取选中的项 var comboBox sender as System.Windows.Controls.ComboBox; if (comboBox ! null) { var selectedItem comboBox.SelectedItem as chain_combox; if (selectedItem ! null) { MessageBox.Show(selectedItem.Name.ToString()); } } }运行结果: