博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wpf中ListBox的选中项与ComboBox间的绑定
阅读量:6830 次
发布时间:2019-06-26

本文共 7228 字,大约阅读时间需要 24 分钟。

产品类:

public  class Product:NotificationObject    {        private int productID;        public int ProductID        {            get { return productID; }            set { productID = value;            this.RaisePropertyChanged(()=>this.ProductID);            }        }        private string productName;        public string ProductName        {            get { return productName; }            set { productName = value;            RaisePropertyChanged(()=>this.ProductName);            }        }        private double unitPrice;        public double UnitPrice        {            get { return unitPrice; }            set { unitPrice = value;            this.RaisePropertyChanged(()=>this.UnitPrice);            }        }        private int categoryID;        public int CategoryID        {            get { return categoryID; }            set { categoryID = value;            RaisePropertyChanged(()=>this.CategoryID);            }        }            }

引用了Microsoft.Practices.Prism.dll,Using了Microsoft.Practices.Prism.ViewModel名称空间,继承NotificationObject类实现更改通知

产品分类的类:

public class Categories:NotificationObject    {        private int categoryID;        public int CategoryID        {            get { return categoryID; }            set             {                 categoryID = value;                this.RaisePropertyChanged(()=>this.CategoryID);            }        }        private string categoryName;        public string CategoryName        {            get { return categoryName; }            set { categoryName = value;            this.RaisePropertyChanged(()=>this.CategoryName);            }        }        private string description;        public string Description        {            get { return description; }            set { description = value;            this.RaisePropertyChanged(()=>this.Description);            }        }            }

后台代码:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.Collections.ObjectModel;using BingingComboBox.Models;using System.ComponentModel;namespace BingingComboBox{    ///     /// MainWindow.xaml 的交互逻辑    ///     public partial class MainWindow : Window, INotifyPropertyChanged    {        //实现INotifyPropertyChanged接口(更改通知)        public event PropertyChangedEventHandler PropertyChanged;        public void RaisePropertyChanged(string propName)        {            if (this.PropertyChanged != null)            {                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propName));            }        }        private Product selectedProduct;        //listBox选中项        public Product SelectedProduct        {            get { return selectedProduct; }            set { selectedProduct = value;            RaisePropertyChanged("SelectedProduct");            }        }               //产品类别集合初始化       private ObservableCollection
categoryList = new ObservableCollection
{ new Categories{ CategoryID=1,CategoryName="Beverages",Description="Soft drinks, coffees, teas, beers, and ales"}, new Categories{ CategoryID=2,CategoryName="Condiments",Description="Sweet and savory sauces, relishes, spreads, and seasonings"}, new Categories{ CategoryID=3,CategoryName="Confections",Description="Desserts, candies, and sweet breads"}, new Categories{ CategoryID=4,CategoryName="Dairy Products",Description="Cheeses"}, new Categories{ CategoryID=5,CategoryName="Grains/Cereals",Description="Breads, crackers, pasta, and cereal"}, new Categories{ CategoryID=6,CategoryName="Meat/Poultry",Description="Prepared meats"}, new Categories{ CategoryID=7,CategoryName="Produce",Description="Dried fruit and bean curd"}, new Categories{ CategoryID= 8,CategoryName="Seafood",Description="Seaweed and fish"} }; private ObservableCollection
productListList; ///
/// 产品集合 /// public ObservableCollection
ProductList { get { if (productListList == null) { productListList = new ObservableCollection
(); } return productListList; } set { productListList = value; } } public MainWindow() { InitializeComponent(); CollectionViewSource categoriesTypeViewSource = (CollectionViewSource)this.FindResource("categoriesTypeViewSource"); categoriesTypeViewSource.Source = categoryList;//用于绑定产品类别的ComboBox的ItemSource CollectionViewSource productsViewSource = (CollectionViewSource)this.FindResource("productsViewSource"); productsViewSource.Source = GetProducts();//用于绑定产品列表ListBox的ItemSource } ///
/// 返回产品列表 /// ///
public ObservableCollection
GetProducts() { ObservableCollection
productList = new ObservableCollection
() { new Product{ProductID=1,ProductName="Chai",UnitPrice=18.00,CategoryID=1}, new Product{ProductID=2,ProductName="Aniseed Syrup",UnitPrice=10.00,CategoryID=2}, new Product{ProductID=3,ProductName="Teatime Chocolate Biscuits", UnitPrice=18.00,CategoryID=3}, new Product{ProductID=4,ProductName="Raclette Courdavault", UnitPrice=25, CategoryID=4}, new Product{ProductID=5,ProductName="Ravioli Angelo", UnitPrice=55,CategoryID=5}, }; return productList; } ///
/// 弹出框显示产品列表选中项(产品)的信息,用来验证下拉产品分类comboBox, /// 更改产品分类时,产品列表选中项的产品分类id是否改变 /// ///
///
private void Button_Click(object sender, RoutedEventArgs e) { if (lbProducts.SelectedIndex > -1 && lbProducts.SelectedItem != null) { Product p = lbProducts.SelectedItem as Product; string msg = string.Format("选中的产品{0}:的单价为{1},类别id为:{2}", p.ProductName, p.UnitPrice, p.CategoryID); MessageBox.Show(msg); } } }}

XAML:

运行效果:

点击“Show”按钮,show出初始值:

下拉产品分类下拉框ComboBox:

点击“Show”按钮,弹出修改后的信息,产品分类id改变:

转载于:https://www.cnblogs.com/527289276qq/p/4357428.html

你可能感兴趣的文章
Qore Oracle Module 2.2 发布
查看>>
MoonScript 0.2.2 发布,基于 Lua 的脚本语言
查看>>
assertThat使用方法
查看>>
2013年11月11日工商银行笔试总结
查看>>
Qt之问题求助——当VS遇到“向Pro中添加代码”怎么办?
查看>>
使用reserve函数避免vector和string的内存重新分配
查看>>
ADO.NET(内含存储过程讲解)
查看>>
利用TreeView实现C#工具箱效果
查看>>
PyTalk : a Jabber Client un Python using xmpppy and PyQt4
查看>>
C++类构造函数初始化列表(转)
查看>>
13最佳WordPress的维护插件
查看>>
Missing Screenshot 的解决方案
查看>>
jQuery:1.5.4.3,表格变色(单击行,把当行的单选按钮(radio)设为选中状态,并应用当前样式)...
查看>>
oracle11gR2安装示例数据库
查看>>
解决ssh的"Write failed: Broken pipe"问题
查看>>
Java 网络编程(五) 使用TCP/IP的套接字(Socket)进行通信
查看>>
拒绝alert调试js,浏览器调试js大全(火狐firefox浏览器,谷歌chrome 浏览器,微软ie9浏览器等)...
查看>>
《深入理解Nginx》阅读与实践(三):使用upstream和subrequest访问第三方服务
查看>>
NGUI:HUD Text(头顶伤害漂浮文字)
查看>>
HTML/CSS/Javascript代码在线压缩、格式化(美化)工具
查看>>