产品类:

  1. public class Product:NotificationObject
  2. {
  3. private int productID;
  4.  
  5. public int ProductID
  6. {
  7. get { return productID; }
  8. set { productID = value;
  9. this.RaisePropertyChanged(()=>this.ProductID);
  10. }
  11. }
  12.  
  13. private string productName;
  14.  
  15. public string ProductName
  16. {
  17. get { return productName; }
  18. set { productName = value;
  19. RaisePropertyChanged(()=>this.ProductName);
  20. }
  21. }
  22.  
  23. private double unitPrice;
  24.  
  25. public double UnitPrice
  26. {
  27. get { return unitPrice; }
  28. set { unitPrice = value;
  29. this.RaisePropertyChanged(()=>this.UnitPrice);
  30. }
  31. }
  32.  
  33. private int categoryID;
  34.  
  35. public int CategoryID
  36. {
  37. get { return categoryID; }
  38. set { categoryID = value;
  39. RaisePropertyChanged(()=>this.CategoryID);
  40. }
  41. }
  42.  
  43. }

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

产品分类的类:

  1. public class Categories:NotificationObject
  2. {
  3. private int categoryID;
  4.  
  5. public int CategoryID
  6. {
  7. get { return categoryID; }
  8. set
  9. {
  10. categoryID = value;
  11. this.RaisePropertyChanged(()=>this.CategoryID);
  12. }
  13. }
  14.  
  15. private string categoryName;
  16.  
  17. public string CategoryName
  18. {
  19. get { return categoryName; }
  20. set { categoryName = value;
  21. this.RaisePropertyChanged(()=>this.CategoryName);
  22. }
  23. }
  24.  
  25. private string description;
  26.  
  27. public string Description
  28. {
  29. get { return description; }
  30. set { description = value;
  31. this.RaisePropertyChanged(()=>this.Description);
  32. }
  33. }
  34.  
  35. }

后台代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.Collections.ObjectModel;
  15. using BingingComboBox.Models;
  16. using System.ComponentModel;
  17.  
  18. namespace BingingComboBox
  19. {
  20.  
  21. /// <summary>
  22. /// MainWindow.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class MainWindow : Window, INotifyPropertyChanged
  25. {
  26. //实现INotifyPropertyChanged接口(更改通知)
  27. public event PropertyChangedEventHandler PropertyChanged;
  28. public void RaisePropertyChanged(string propName)
  29. {
  30. if (this.PropertyChanged != null)
  31. {
  32. this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propName));
  33. }
  34. }
  35.  
  36. private Product selectedProduct;
  37. //listBox选中项
  38. public Product SelectedProduct
  39. {
  40. get { return selectedProduct; }
  41. set { selectedProduct = value;
  42. RaisePropertyChanged("SelectedProduct");
  43. }
  44. }
  45.  
  46. //产品类别集合初始化
  47. private ObservableCollection<Categories> categoryList = new ObservableCollection<Categories>
  48. {
  49. new Categories{ CategoryID=,CategoryName="Beverages",Description="Soft drinks, coffees, teas, beers, and ales"},
  50. new Categories{ CategoryID=,CategoryName="Condiments",Description="Sweet and savory sauces, relishes, spreads, and seasonings"},
  51. new Categories{ CategoryID=,CategoryName="Confections",Description="Desserts, candies, and sweet breads"},
  52. new Categories{ CategoryID=,CategoryName="Dairy Products",Description="Cheeses"},
  53. new Categories{ CategoryID=,CategoryName="Grains/Cereals",Description="Breads, crackers, pasta, and cereal"},
  54. new Categories{ CategoryID=,CategoryName="Meat/Poultry",Description="Prepared meats"},
  55. new Categories{ CategoryID=,CategoryName="Produce",Description="Dried fruit and bean curd"},
  56. new Categories{ CategoryID= ,CategoryName="Seafood",Description="Seaweed and fish"}
  57. };
  58.  
  59. private ObservableCollection<Product> productListList;
  60. /// <summary>
  61. /// 产品集合
  62. /// </summary>
  63. public ObservableCollection<Product> ProductList
  64. {
  65. get
  66. {
  67. if (productListList == null)
  68. {
  69. productListList = new ObservableCollection<Product>();
  70. }
  71. return productListList;
  72. }
  73. set
  74. {
  75. productListList = value;
  76. }
  77. }
  78.  
  79. public MainWindow()
  80. {
  81. InitializeComponent();
  82.  
  83. CollectionViewSource categoriesTypeViewSource = (CollectionViewSource)this.FindResource("categoriesTypeViewSource");
  84. categoriesTypeViewSource.Source = categoryList;//用于绑定产品类别的ComboBox的ItemSource
  85. CollectionViewSource productsViewSource = (CollectionViewSource)this.FindResource("productsViewSource");
  86. productsViewSource.Source = GetProducts();//用于绑定产品列表ListBox的ItemSource
  87. }
  88.  
  89. /// <summary>
  90. /// 返回产品列表
  91. /// </summary>
  92. /// <returns></returns>
  93. public ObservableCollection<Product> GetProducts()
  94. {
  95. ObservableCollection<Product> productList = new ObservableCollection<Product>()
  96. {
  97. new Product{ProductID=,ProductName="Chai",UnitPrice=18.00,CategoryID=},
  98. new Product{ProductID=,ProductName="Aniseed Syrup",UnitPrice=10.00,CategoryID=},
  99. new Product{ProductID=,ProductName="Teatime Chocolate Biscuits", UnitPrice=18.00,CategoryID=},
  100. new Product{ProductID=,ProductName="Raclette Courdavault", UnitPrice=, CategoryID=},
  101. new Product{ProductID=,ProductName="Ravioli Angelo", UnitPrice=,CategoryID=},
  102. };
  103. return productList;
  104. }
  105. /// <summary>
  106. /// 弹出框显示产品列表选中项(产品)的信息,用来验证下拉产品分类comboBox,
  107. /// 更改产品分类时,产品列表选中项的产品分类id是否改变
  108. /// </summary>
  109. /// <param name="sender"></param>
  110. /// <param name="e"></param>
  111. private void Button_Click(object sender, RoutedEventArgs e)
  112. {
  113. if (lbProducts.SelectedIndex > - && lbProducts.SelectedItem != null)
  114. {
  115. Product p = lbProducts.SelectedItem as Product;
  116. string msg = string.Format("选中的产品{0}:的单价为{1},类别id为:{2}", p.ProductName, p.UnitPrice, p.CategoryID);
  117. MessageBox.Show(msg);
  118. }
  119. }
  120.  
  121. }
  122. }

XAML:

  1. <Window x:Class="BingingComboBox.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="MainWindow" Height="350" Width="525" DataContext="{Binding RelativeSource={RelativeSource Self}}">
  5. <Window.Resources>
  6. <CollectionViewSource x:Key="categoriesTypeViewSource"/>
  7. <CollectionViewSource x:Key="productsViewSource"/>
  8. </Window.Resources>
  9. <Grid>
  10. <Grid>
  11. <Grid.ColumnDefinitions>
  12. <ColumnDefinition Width="Auto"/>
  13. <ColumnDefinition/>
  14. </Grid.ColumnDefinitions>
  15. <ListBox x:Name="lbProducts" SelectedItem="{Binding Path=SelectedProduct, Mode= TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Source={StaticResource ResourceKey=productsViewSource}}">
  16. <ListBox.ItemTemplate>
  17. <DataTemplate>
  18. <TextBlock Text="{Binding Path=ProductName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
  19. </DataTemplate>
  20. </ListBox.ItemTemplate>
  21. </ListBox>
  22. <Grid Grid.Column="1" DataContext="{Binding Path=SelectedProduct, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
  23. <Grid.RowDefinitions>
  24. <RowDefinition Height="Auto"/>
  25. <RowDefinition Height="Auto"/>
  26. <RowDefinition Height="Auto"/>
  27. <RowDefinition Height="Auto"/>
  28. </Grid.RowDefinitions>
  29. <Grid.ColumnDefinitions>
  30. <ColumnDefinition/>
  31. <ColumnDefinition/>
  32. </Grid.ColumnDefinitions>
  33. <TextBlock Text="产品名称:"/>
  34. <TextBox Grid.Column="1" Text="{Binding Path=ProductName}"/>
  35. <TextBlock Grid.Row="1" Text="产品类别:"/>
  36. <ComboBox Grid.Row="1" Grid.Column="1"
  37. ItemsSource="{Binding Source={StaticResource ResourceKey=categoriesTypeViewSource}}"
  38. DisplayMemberPath="CategoryName"
  39. SelectedValuePath="CategoryID"
  40. SelectedValue="{Binding Path=CategoryID,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
  41. />
  42. <TextBlock Grid.Row="2" Text="产品单价:"/>
  43. <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Path=UnitPrice}"/>
  44. <Button Grid.Row="3" Grid.Column="1" Content="Show" Click="Button_Click"/>
  45. </Grid>
  46. </Grid>
  47. </Grid>
  48. </Window>

运行效果:

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

下拉产品分类下拉框ComboBox:

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

wpf中ListBox的选中项与ComboBox间的绑定的更多相关文章

  1. WPF中ListBox的项ListBoxItem被选中的时候Background变化

    使用WPF 中ListBox,点击ListBoxItem的时候,自定义它的背景色,曾经在网上找了一些方法, 不是很理想,后来在StackOverflow上找到了,贴出代码和效果图: 效果图:

  2. WPF中ListBox滚动时的缓动效果

    原文:WPF中ListBox滚动时的缓动效果 上周工作中遇到的问题: 常规的ListBox在滚动时总是一格格的移动,感觉上很生硬. 所以想要实现类似Flash中的那种缓动的效果,使ListBox滚动时 ...

  3. Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定)

    原文:Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定) ------------------------------ ...

  4. WPF中实现多选ComboBox控件

    在WPF中实现带CheckBox的ComboBox控件,让ComboBox控件可以支持多选. 将ComboBox的ItemsSource属性Binding到一个Book的集合, public clas ...

  5. WPF学习笔记:获取ListBox的选中项

    有代码有J8: UI <UserControl x:Class="UnitViews.UserListUV" xmlns="http://schemas.micro ...

  6. 转:WPF中ListBox的创建和多种绑定用法

    先从最容易的开始演示ListBox控件的创建. Adding ListBox Items下面的代码是向ListBox控件中添加多项ListBoxItem集合.XAML代码如下:<ListBox ...

  7. WPF中ListBox /ListView如何改变选中条背景颜色

    适用ListBox /ListView WPF中LISTVIEW如何改变选中条背景颜色 https://www.cnblogs.com/sjqq/p/7828119.html

  8. WPF中ListBox的绑定

    WPF中列表式控件派生自ItemsControl类,继承了ItemsSource属性.ItemsSource属性可以接收一个IEnumerable接口派生类的实例作为自己的值(所有可被迭代遍历的集合都 ...

  9. 在WPF中使用变通方法实现枚举类型的XAML绑定

    问题缘起 WPF的分层结构为编程带来了极大便利,XAML绑定是其最主要的特征.在使用绑定的过程中,大家都普遍的发现枚举成员的绑定是个问题.一般来说,枚举绑定多出现于与ComboBox配合的情况,此时我 ...

随机推荐

  1. 千万PV级别WEB站点架构设计

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://sofar.blog.51cto.com/353572/1369762 高性能与多 ...

  2. 2014web面试题

    面试题目会根据你的等级和职位变化,入门级到专家级:范围↑.深度↑.方向↑; 类型: 技术视野.项目细节.理论知识型题,算法题,开放性题,案例题. 进行追问,可以确保问到你开始不懂或者面试官开始不懂为止 ...

  3. LoadLibrary 失败 GetLastError 126

    这个错误可能是 Load的库依赖另外的库,而其依赖的库不存在,也会返回这个错误!

  4. chapter8_3 c代码和错误

    1.C代码 Lua提供的所有关于动态链接的功能都集中在一个函数中,即package.loadlib. 该函数有两个字符串参数:动态库的完整路径和一个函数名称: local path = "/ ...

  5. NOIP2005-普及组复赛-第三题-采药

    题目描述 Description 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给他出了一个难题.医师把他带到一个到处都是草药的山 ...

  6. Android JNI的使用浅析

    介绍JNI的好文章: http://blog.csdn.net/yuanzeyao/article/details/42418977 JNI技术对于多java开发的朋友相信并不陌生,即(java na ...

  7. shell 提取字符串

    记录一下: 我们可以用  ${ }  分别替换获得不同的值: file=/dir1/dir2/dir3/my.file.txt ${file#*/}:拿掉第一条  /  及其左边的字符串:dir1/d ...

  8. visible绑定(The "visible" binding)

    对visible进行绑定可以控制元素的显示和隐藏. 示例: <div data-bind="visible: shouldShowMessage"> You will ...

  9. 从MySQL全库备份中恢复某个库和某张表【转】

    从MySQL全库备份中恢复某个库和某张表 一.全库备份-A [root@mha2 backup]#mysqldump -uroot -p123456 --default-character-set=u ...

  10. mysql表备份及还原

    备份 导出数据库所有表结构 ? 1 mysqldump -uroot -ppassword -d dbname > db.sql 导出数据库某个表结构 ? 1 mysqldump -uroot ...