产品类:

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
{ /// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
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<Categories> categoryList = new ObservableCollection<Categories>
{
new Categories{ CategoryID=,CategoryName="Beverages",Description="Soft drinks, coffees, teas, beers, and ales"},
new Categories{ CategoryID=,CategoryName="Condiments",Description="Sweet and savory sauces, relishes, spreads, and seasonings"},
new Categories{ CategoryID=,CategoryName="Confections",Description="Desserts, candies, and sweet breads"},
new Categories{ CategoryID=,CategoryName="Dairy Products",Description="Cheeses"},
new Categories{ CategoryID=,CategoryName="Grains/Cereals",Description="Breads, crackers, pasta, and cereal"},
new Categories{ CategoryID=,CategoryName="Meat/Poultry",Description="Prepared meats"},
new Categories{ CategoryID=,CategoryName="Produce",Description="Dried fruit and bean curd"},
new Categories{ CategoryID= ,CategoryName="Seafood",Description="Seaweed and fish"}
}; private ObservableCollection<Product> productListList;
/// <summary>
/// 产品集合
/// </summary>
public ObservableCollection<Product> ProductList
{
get
{
if (productListList == null)
{
productListList = new ObservableCollection<Product>();
}
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
} /// <summary>
/// 返回产品列表
/// </summary>
/// <returns></returns>
public ObservableCollection<Product> GetProducts()
{
ObservableCollection<Product> productList = new ObservableCollection<Product>()
{
new Product{ProductID=,ProductName="Chai",UnitPrice=18.00,CategoryID=},
new Product{ProductID=,ProductName="Aniseed Syrup",UnitPrice=10.00,CategoryID=},
new Product{ProductID=,ProductName="Teatime Chocolate Biscuits", UnitPrice=18.00,CategoryID=},
new Product{ProductID=,ProductName="Raclette Courdavault", UnitPrice=, CategoryID=},
new Product{ProductID=,ProductName="Ravioli Angelo", UnitPrice=,CategoryID=},
};
return productList;
}
/// <summary>
/// 弹出框显示产品列表选中项(产品)的信息,用来验证下拉产品分类comboBox,
/// 更改产品分类时,产品列表选中项的产品分类id是否改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click(object sender, RoutedEventArgs e)
{
if (lbProducts.SelectedIndex > - && 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:

<Window x:Class="BingingComboBox.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" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
<CollectionViewSource x:Key="categoriesTypeViewSource"/>
<CollectionViewSource x:Key="productsViewSource"/>
</Window.Resources>
<Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ListBox x:Name="lbProducts" SelectedItem="{Binding Path=SelectedProduct, Mode= TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Source={StaticResource ResourceKey=productsViewSource}}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=ProductName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Grid Grid.Column="1" DataContext="{Binding Path=SelectedProduct, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="产品名称:"/>
<TextBox Grid.Column="1" Text="{Binding Path=ProductName}"/>
<TextBlock Grid.Row="1" Text="产品类别:"/>
<ComboBox Grid.Row="1" Grid.Column="1"
ItemsSource="{Binding Source={StaticResource ResourceKey=categoriesTypeViewSource}}"
DisplayMemberPath="CategoryName"
SelectedValuePath="CategoryID"
SelectedValue="{Binding Path=CategoryID,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
/>
<TextBlock Grid.Row="2" Text="产品单价:"/>
<TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Path=UnitPrice}"/>
<Button Grid.Row="3" Grid.Column="1" Content="Show" Click="Button_Click"/>
</Grid>
</Grid>
</Grid>
</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. Spring Aop详尽教程

    一.概念 AOP(Aspect Oriented Programming):面向切面编程. 面向切面编程(也叫面向方面编程),是目前软件开发中的一个热点,也是Spring框架中的一个重要内容.利用AO ...

  2. C# Monads的实现(一)

    了解Haskell语言的朋友都知道它是一门纯函数式编程,输出结果只跟输入参数相关,这导致Haskell不能有输入输出函数,因为不同的环境下,输入相同,但输出可能有所不同.Haskell语言中,变量的值 ...

  3. 自定义MVC框架(一)-(没有基于xml的)

    0.创建oracle的sql语句如下 --创建表 create table userinfo(id number primary key,uname varchar2(20),password var ...

  4. asp.net 批量删除

    直接上代码: 1.页面部分 <script type="text/javascript" src="http://code.jquery.com/jquery-1. ...

  5. boost 编译依赖库

    正则表达式 icu-devel python python-devel bzip2-devel ./b2 variant=release link=shared threading=multi run ...

  6. 处理Properties文件中key包含空格的情况

    在这个互联网信息共享的时代,好处是一个问题的很多解决方案都可以从网络上得到,不好的一点就是很多人喜欢复制粘贴也不注明转载出处,不尊重别人的劳动成果,不假思索地把别人的原创复制到自己的博客然后发布,请大 ...

  7. 利用yield关键字输出杨辉三角

    最近学习了下python,发现里面也有yield的用法,本来对C#里的yield不甚了解,但是通过学习python,对于C#的yield理解更深了!! 不多说了,小学生水平的表达能力伤不起.... 直 ...

  8. [ An Ac a Day ^_^ ] CodeForces 677B Vanya and Food Processor 模拟

    题意: 你有一个榨汁机 还有n个土豆 榨汁机可以容纳h高的土豆 每秒可以榨k高的东西 问按顺序榨完土豆要多久 思路: 直接模拟 一开始以为是最短时间排了个序 后来发现多余了…… #include< ...

  9. 直接粘贴代码到网络上:command-line pastebins

    软件作用 直接把管道里面的文字内容传到网站上面,然后反馈一个地址可以读取内容. 同类软件 wgetpaste dpaste pastebin pasteie 用法 介绍wgetpaste为例: GEN ...

  10. UISwitch 开关控件

    UISwitch iOS中的开关控件,只有两种状态,打开或关闭. aSwitch.tintColor = [UIColor redColor]; //关闭状态下的渲染颜色 aSwitch.onTint ...