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
using System;

using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.DataAnnotations;
namespace SLYlbtechApp.Access
{
/// <summary>
/// 产品类
/// </summary>
public class Product
{
int productId;
/// <summary>
/// 编号【PK】
/// </summary>
public int ProductId
{
get { return productId; }
set { productId = value; }
}
string productName;
/// <summary>
/// 产品名称
/// </summary>
public string ProductName
{
get
{
return productName;
}
set
{
if (value=="") throw new ArgumentException("不能为空");
productName = value;
}
}
string quantityPerUnit;
/// <summary>
/// 产品规格
/// </summary>
public string QuantityPerUnit
{
get { return quantityPerUnit; }
set { quantityPerUnit = value; }
}
/// <summary>
/// 单位价格
/// 注意 不要使用 decimal 类型,在 UserControl.Resounrce 资源绑定是引发类型转换异常
/// </summary>
double unitPrice;
[Display(Name="价格",Description="价格不能小于0")]
public double UnitPrice
{
get { return unitPrice; }
set
{
if (value < ) throw new ArgumentException("不能小于0");
unitPrice = value;
}
}
string description;
/// <summary>
/// 描述
/// </summary>
public string Description
{
get { return description; }
set { description = value; }
}
string productImagePath;
/// <summary>
/// 产品图片地址
/// </summary>
public string ProductImagePath
{
get { return productImagePath; }
set { productImagePath = value; }
}
string categoryName;
/// <summary>
/// 类别名称
/// </summary>
public string CategoryName
{
get { return categoryName; }
set { categoryName = value; }
} /// <summary>
/// 空参构造
/// </summary>
public Product()
{ }
/// <summary>
/// 全参构造
/// </summary>
/// <param name="productId"></param>
/// <param name="productName"></param>
/// <param name="quantityPerUnit"></param>
/// <param name="unitPrice"></param>
/// <param name="description"></param>
/// <param name="productImagePath"></param>
/// <param name="categoryName"></param>
public Product(int productId, string productName, string quantityPerUnit, double unitPrice, string description
, string productImagePath, string categoryName)
{
ProductId = productId;
ProductName=productName;
QuantityPerUnit=quantityPerUnit;
UnitPrice=unitPrice;
Description=description; ProductImagePath=productImagePath;
CategoryName=categoryName;
} /// <summary>
/// 获取全部产品
/// </summary>
/// <returns></returns>
public static IList<Product> GetAll()
{
IList<Product> dals = new List<Product>();
string categoryName = string.Empty;
string productImagePath = string.Empty;
string description = "嗟夫!草木无情,有时飘零。人为动物,惟物之灵。百忧感其心,万事劳其形,有动于中,必摇其精。而况思其力之所不及,忧其智之所不能,宜其渥然丹者为槁木,黟然黑者为星星。奈何以非金石之质,欲与草木而争荣?念谁为之戕贼,亦何恨乎秋声!";
#region Add Product #region 饮料
categoryName = "饮料";
productImagePath = "../Images/pencil.jpg"; dals.Add(new Product()
{
ProductId = ,
ProductName = "啤酒",
QuantityPerUnit = "1箱*6听",
UnitPrice = 10d,
CategoryName = categoryName
,
ProductImagePath = productImagePath,
Description = description
}); dals.Add(new Product()
{
ProductId = ,
ProductName = "绿茶",
QuantityPerUnit = "500ml",
UnitPrice = 3d,
CategoryName = categoryName
,
ProductImagePath = productImagePath,
Description = description
}); dals.Add(new Product()
{
ProductId = ,
ProductName = "红茶",
QuantityPerUnit = "500ml",
UnitPrice = 3d,
CategoryName = categoryName
,
ProductImagePath = productImagePath,
Description = description
});
dals.Add(new Product()
{
ProductId = ,
ProductName = "纯净水",
QuantityPerUnit = "500ml",
UnitPrice = 1.5d,
CategoryName = categoryName
,
ProductImagePath = productImagePath,
Description = description
});
#endregion #region 零食
categoryName = "零食";
productImagePath = "../Images/pencil2.jpg"; dals.Add(new Product()
{
ProductId = ,
ProductName = "奥利奥巧克力饼干",
QuantityPerUnit = "10 boxes x 20 bags",
UnitPrice = 30d,
CategoryName = categoryName
,
ProductImagePath = productImagePath,
Description = description
}); dals.Add(new Product()
{
ProductId = ,
ProductName = "玻璃 海苔",
QuantityPerUnit = "20g",
UnitPrice = 13d,
CategoryName = categoryName
,
ProductImagePath = productImagePath,
Description = description
}); dals.Add(new Product()
{
ProductId = ,
ProductName = "好丽友 薯片",
QuantityPerUnit = "100g",
UnitPrice = 3d,
CategoryName = categoryName
,
ProductImagePath = productImagePath,
Description = description
});
dals.Add(new Product()
{
ProductId = ,
ProductName = "统一 老坛酸菜面",
QuantityPerUnit = "1 盒",
UnitPrice = 8.5d,
CategoryName = categoryName
,
ProductImagePath = productImagePath,
Description = description
});
#endregion #endregion
return dals;
}
/// <summary>
/// GetModel
/// </summary>
/// <returns></returns>
public static Product GetModel()
{
Product dal = null; string categoryName = string.Empty;
string productImagePath = string.Empty;
string description = "嗟夫!草木无情,有时飘零。人为动物,惟物之灵。百忧感其心,万事劳其形,有动于中,必摇其精。而况思其力之所不及,忧其智之所不能,宜其渥然丹者为槁木,黟然黑者为星星。奈何以非金石之质,欲与草木而争荣?念谁为之戕贼,亦何恨乎秋声!"; categoryName = "饮料";
productImagePath = "Images/pencil.jpg";
dal = new Product()
{
ProductId = ,
ProductName = "啤酒",
QuantityPerUnit = "1箱*6听",
UnitPrice=3.5,
CategoryName = categoryName
,
ProductImagePath = productImagePath,
Description = description
};
//dal._unitPrice = 10d;
return dal;
}
/// <summary>
/// GetModel
/// </summary>
/// <param name="id">产品编号</param>
/// <returns></returns>
public static Product GetModel(int id)
{
Product dal = null;
IList<Product> dals = GetAll();
try
{
dal = dals.Single(p => p.ProductId == id);
}
catch { }
finally
{
dals = null;
}
return dal;
}
}
}

/DataBinding/PriceConverter.cs

using System;

using System.Windows.Data;
using System.Globalization;
namespace SLYlbtechApp.DataBinding
{
/// <summary>
/// 价格格式转换器
/// </summary>
public class PriceConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture)
{
double price = (double)value;
return price.ToString("C", culture);
} public object ConvertBack(object value, Type targetType, object parameter,
CultureInfo culture)
{
string price = value.ToString();
double result;
if (double.TryParse(price, NumberStyles.Any, culture, out result))
{
return result;
}
return value;
}
}
}

4,

1.B, Calling a Data Service(调用一个数据服务)【测试数据】返回顶部
1,
2,
2.1/3,[无]
2.2/3,

<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="115"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="100"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Margin="7">Product Id:</TextBlock>
<TextBox Grid.Row="0" Grid.Column="1" Margin="5" Name="txtProductId"></TextBox>
<Button Grid.Row="0" Grid.Column="2" Margin="5" Name="btnSearchProduct"
Content="Get Product" Click="btnSearchProduct_Click"></Button>
</Grid>
<Grid Grid.Row="1" x:Name="gridDetailProduct" Background="LightGray" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="115"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" Margin="7">Product Name:</TextBlock>
<TextBox Grid.Row="0" Grid.Column="1" Margin="5" Text="{Binding ProductName}"></TextBox> <TextBlock Grid.Row="1" Grid.Column="0" Margin="7">Quantity Per Unit:</TextBlock>
<TextBox Grid.Row="1" Grid.Column="1" Margin="5" Text="{Binding QuantityPerUnit}"></TextBox> <TextBlock Grid.Row="2" Grid.Column="0" Margin="7">Unit Price:</TextBlock>
<TextBox Grid.Row="2" Grid.Column="1" Margin="5" Text="{Binding UnitPrice}"></TextBox> <TextBlock Grid.Row="3" Grid.Column="0" Margin="7">Description:</TextBlock>
<TextBox Grid.Row="3" Grid.Column="1" Grid.RowSpan="2" Margin="5" Height="100" Text="{Binding Description}" TextWrapping="Wrap" ></TextBox>
</Grid>
</Grid>

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 = ;
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,

<UserControl.Resources>
<local:PriceConverter x:Key="PriceConverter"></local:PriceConverter>
<!--<local:ImagePathConverter x:Key="ImagePathConverter"></local:ImagePathConverter>-->
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="115"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="100"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListBox Grid.Row="0" Grid.ColumnSpan="3" Margin="5" Name="lstProduct" DisplayMemberPath="ProductName"></ListBox>
<Button Grid.Row="1" Grid.Column="2" Margin="5" Name="btnSearchProduct" Content="Get Product" Click="btnSearchProduct_Click"></Button>
</Grid>
<Grid Grid.Row="1" x:Name="gridDetailProduct" Background="LightGray" Height="800" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="115"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" Margin="7">Product Name:</TextBlock>
<TextBox Grid.Row="0" Grid.Column="1" Margin="5" Text="{Binding ProductName}"></TextBox> <TextBlock Grid.Row="1" Grid.Column="0" Margin="7">Quantity Per Unit:</TextBlock>
<TextBox Grid.Row="1" Grid.Column="1" Margin="5" Text="{Binding QuantityPerUnit}"></TextBox> <TextBlock Grid.Row="2" Grid.Column="0" Margin="7">Unit Price:</TextBlock>
<TextBox Grid.Row="2" Grid.Column="1" Margin="5" Text="{Binding UnitPrice,Converter={StaticResource PriceConverter}}"></TextBox> <TextBlock Grid.Row="3" Grid.Column="0" Margin="7">Description:</TextBlock>
<TextBox Grid.Row="3" Grid.Column="1" Grid.RowSpan="2" Margin="5" Height="100" Text="{Binding Description}" TextWrapping="Wrap" ></TextBox> <Image Grid.Row="5" Grid.Column="1" Source="{Binding ProductImagePath}" Height="100" Width="100" ></Image>
</Grid>
</Grid>

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 == -)
{
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,

<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition Height="100"></RowDefinition>
</Grid.RowDefinitions>
<ListBox Grid.Row="0" Grid.ColumnSpan="3" Margin="5" Name="lstProduct" DisplayMemberPath="ProductName" SelectionChanged="lstProduct_SelectionChanged"></ListBox>
</Grid>
<Grid Grid.Row="1" x:Name="gridDetailProduct" Background="LightGray" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="115"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" Margin="7">Product Name:</TextBlock>
<TextBox Grid.Row="0" Grid.Column="1" Margin="5" Text="{Binding ProductName}"></TextBox> <TextBlock Grid.Row="1" Grid.Column="0" Margin="7">Quantity Per Unit:</TextBlock>
<TextBox Grid.Row="1" Grid.Column="1" Margin="5" Text="{Binding QuantityPerUnit}"></TextBox> <TextBlock Grid.Row="2" Grid.Column="0" Margin="7">Unit Price:</TextBlock>
<TextBox Grid.Row="2" Grid.Column="1" Margin="5" Text="{Binding UnitPrice}"></TextBox> <TextBlock Grid.Row="3" Grid.Column="0" Margin="7">Description:</TextBlock>
<TextBox Grid.Row="3" Grid.Column="1" Grid.RowSpan="2" Margin="5" Height="100" Text="{Binding Description}" TextWrapping="Wrap" ></TextBox>
</Grid>
</Grid>

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,
1.E,返回顶部
 
作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

SilverLight-DataBinding:二、Bingding to a Collection Objects(绑定一个集合对象)的更多相关文章

  1. SilverLight: 数据绑定(1)-绑定到数据对象

    ylbtech-SilverLight-DataBinding: Binding to Data Objects(绑定到数据对象) 1.A, Building  a Data Object(创建一个数 ...

  2. Silverlight DataBinding Converter:根据binding对象调整显示

    Silverlight DataBinding Converter:根据binding对象调整显示 我希望写一系列关于Silverlight DataBinding的文章,分别讲解Silverligh ...

  3. JavaScript学习总结(二)——闭包、IIFE、apply、函数与对象

    一.闭包(Closure) 1.1.闭包相关的问题 请在页面中放10个div,每个div中放入字母a-j,当点击每一个div时显示索引号,如第1个div显示0,第10个显示9:方法:找到所有的div, ...

  4. Java之Collection接口(单列集合根接口)

    集合概述 集合到底是什么呢?集合:集合是java中提供的一种容器,可以用来存储多个数据 集合和数组既然都是容器,它们有啥区别呢? 区别1: 数组的长度是固定的. 集合的长度是可变的. 区别2:  数组 ...

  5. java集合类 collection接口,List集合

    java集合类:collection接口,List集合 在java.util包中提供了一些集合类,集合类又被称为容器,常用的有List集合,Set集合,Map集合.下面将介绍collection接口和 ...

  6. JavaScript学习笔记(二)——闭包、IIFE、apply、函数与对象

    一.闭包(Closure) 1.1.闭包相关的问题 请在页面中放10个div,每个div中放入字母a-j,当点击每一个div时显示索引号,如第1个div显示0,第10个显示9:方法:找到所有的div, ...

  7. Java collection 的一些介绍 集合

    collections主要提供一些,排序的算法,随机的,反向的,  collection 是容器的祖先接口 线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经为我们提供了一系列相应 ...

  8. Qt信号槽机制的实现(面试的感悟,猜测每一个类保存的一个信号和槽的二维表,实际使用函数指针 元对象 还有类型安全的检查设定等等)

    因为面试时问了我这道题,导致我想去了解信号槽到底是如何实现的,于是贴着顺序看了下源码,大致了解了整个框架.网上关于信号槽的文章也很多,但是大部分都是将如何应用的,这里我就写一下我所理解的如何实现吧, ...

  9. 第一百二十一节,JavaScript事件绑定及深入

    JavaScript事件绑定及深入 学习要点: 1.传统事件绑定的问题 2.W3C事件处理函数 3.IE事件处理函数 4.事件对象的其他补充 事件绑定分为两种:一种是传统事件绑定(内联模型,脚本模型) ...

随机推荐

  1. P3391 【模板】文艺平衡树(Splay)新板子

    P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转 ...

  2. python基础学习笔记——字符串方法

    索引和切片: 索引:取出数组s中第3个元素:x=s[2] 切片:用极少的代码将数组元素按需处理的一种方法.切片最少有1个参数,最多有3个参数,演示如下: 我们假设下面所用的数组声明为array=[2, ...

  3. MongoDB学习-->设置通用的自增ID替代ObjectId

    插入mongodb数据时,会为其分配一个随机id,想要设置通用的自增id,可以进行以下操作 1.创建自增序列 package com.tangzhe.autoid; import lombok.Dat ...

  4. Appscan安全漏洞扫描使用(转)

    这里主要分享如何使用AppScan对一大项目的部分功能进行安全扫描. ----------------------------------------------------------------- ...

  5. CI 分页类的使用

    分页本身很简单,无非就是一个 [limit $offset, $length] 的过程. $length 是每页显示的数据量,这个是固定的.要确定的就只有 $offset了. 在CI中的分页类同样要依 ...

  6. TensorFlow——小练习:feed

    feed就是喂入数据 使用feed前必须要有占位符作为操作的对象,在运行操作的时候喂入数据. # _*_coding:utf-8_*_ import tensorflow as tf import n ...

  7. linux下编译静态库openssl

    先编译zlib cmake . -DCMAKE_INSTALL_PREFIX=/depends make make install 然后编译openssl ./config zlib no-rc5 n ...

  8. LibreOJ2241 - 「CQOI2014」排序机械臂

    Portal Description 给出一个\(n(n\leq10^5)\)个数的序列\(\{a_n\}\),对该序列进行\(n\)次操作.若在第\(i\)次操作前第\(i\)小的数在\(p_i\) ...

  9. java面试题之什么是多线程上下文切换

    多线程会共同使用一组计算机上的CPU,而线程数大于给程序分配的CPU数量时,为了让各个线程都有执行的机会,就需要轮流使用CPU.不同的线程切换使用CPU发生的数据切换等就是上下文切换

  10. 【bzoj2440】[中山市选2011]完全平方数 莫比乌斯反演

    Description 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平方数.他觉得这些数看起来很令人难受.由此,他也讨厌所有是完全平方数的正整数倍的数.然而这丝毫不影响他对其他数的热爱.这天是小 ...