ylbtech-SilverLight-DataBinding: Bingding to a Collection Objects(绑定一个集合对象) |
- 1.A, Building a Data Object(创建一个数据对象)
- 1.B, Calling a Data Service(调用一个数据服务)【测试数据】
- 1.C, Binding to a Collection of Objects(绑定一个对象集合)
- 1.D, Binding to a Collection of Objects 2(绑定一个对象集合 2)
1.A, Building a Data Object(创建一个数据对象)返回顶部 |
/Access/Prouct.cs
View Code
/DataBinding/PriceConverter.cs
View Code
4,
1.B, Calling a Data Service(调用一个数据服务)【测试数据】返回顶部 |
1,
2,
2.1/3,[无]
2.2/3,
View Code
2.3/3,
using System.Windows.Controls; using System.Windows; using SLYlbtechApp.Access; namespace SLYlbtechApp.ABindingToDataObjects { public partial class TemplateB1 : UserControl { public TemplateB1() { InitializeComponent(); } //根据产品编号查找产品 private void btnSearchProduct_Click(object sender, System.Windows.RoutedEventArgs e) { int id = 0; if (int.TryParse(this.txtProductId.Text.Trim(), out id)) { Product dal = Product.GetModel(id); if (dal == null) { MessageBox.Show("输入的产品编号不存在"); } else { this.gridDetailProduct.DataContext = dal; } } else { MessageBox.Show("输入的产品编号错误"); } } } }
3,
4,
1.C, Binding to a Collection of Objects(绑定一个对象集合)返回顶部 |
1,
2,
2.1/3,
xmlns:local="clr-namespace:SLYlbtechApp.DataBinding"
2.2/3,
View Code
2.3/3,
using System.Windows.Controls; using System.Windows; using SLYlbtechApp.Access; namespace SLYlbtechApp.ABindingToDataObjects { public partial class TemplateB2 : UserControl { public TemplateB2() { InitializeComponent(); this.lstProduct.ItemsSource = Product.GetAll(); } private void btnSearchProduct_Click(object sender, System.Windows.RoutedEventArgs e) { if (this.lstProduct.SelectedIndex == -1) { MessageBox.Show("请选择列表框中的商品"); } else { //方式一 //Product dal = (Product)this.lstProduct.SelectedItem; //this.gridDetailProduct.DataContext = dal; //方式二 this.gridDetailProduct.DataContext = this.lstProduct.SelectedItem; } } } }
3,
3.A,ListBox 绑定
3.A.Code,
<ListBox Grid.Row="0" Grid.ColumnSpan="3" Margin="5" Name="lstProduct" DisplayMemberPath="ProductName"></ListBox>
3.A.Desc,
DisplayMemberPath[绑定集合里的属性名称]
3.B,产品价格格式转换
3.B.1/3,
xmlns:local="clr-namespace:SLYlbtechApp.DataBinding"
3.B.2/3,
<UserControl.Resources> <local:PriceConverter x:Key="PriceConverter"></local:PriceConverter> </UserControl.Resources>
3.B.3/3,
<TextBox Grid.Row="2" Grid.Column="1" Margin="5" Text="{Binding UnitPrice,Converter={StaticResource PriceConverter}}"></TextBox>
4,
1.D,Binding to a Collection of Objects 2(绑定一个对象集合 2)返回顶部 |
1,
2,
2.1/3,[无]
2.2/3,
View Code
2.3/3,
using System.Windows.Controls; using SLYlbtechApp.Access; namespace SLYlbtechApp.ABindingToDataObjects { public partial class TemplateBb2 : UserControl { public TemplateBb2() { InitializeComponent(); this.lstProduct.ItemsSource = Product.GetAll(); } /// <summary> /// 列表框选项改变事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void lstProduct_SelectionChanged(object sender, SelectionChangedEventArgs e) { //方式一 //Product dal = (Product)this.lstProduct.SelectedItem; //this.gridDetailProduct.DataContext = dal; //方式二 this.gridDetailProduct.DataContext = this.lstProduct.SelectedItem; } } }
3,
3.A, 同上文 D.3.A
4,