SilverLight-DataBinding:二、Bingding to a Collection Objects(绑定一个集合对象)

简介:
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,
1.E,返回顶部

本文转自ylbtech博客园博客,原文链接:http://www.cnblogs.com/ylbtech/p/3427378.html ,如需转载请自行联系原作者
 
相关文章
关于silverlight中如何更新(增删改)集合ItemsSource后更新到UI(Listbox、DataGrid等)
请看微软资料 http://msdn.microsoft.com/zh-cn/library/system.windows.controls.itemscontrol.itemssource%28v=vs.95%29.aspx   具体方案为:将ItermsSource设置为实现 INotifyCollectionChanged 接口的对象,以使集合的更改在 ItemsControl 中反映出来。
934 0
|
开发工具 传感器
Silverlight:双向绑定综合应用-自动更新集合汇总字段
场景:有一家公司(类名:Company),它有N多员工(类名:Employee)。要在界面上用网格显示所有员工的姓名、工资,并且当操作用户在网格里对员工进行增减或修改其工资时,能自动汇总出员工工资的总和并显示出来。
785 0
|
开发工具 数据库
Silverlight:双向绑定综合应用-多集合的依赖绑定
这是上一篇“Silverlight:双向绑定综合应用-自动更新集合汇总字段”的续篇。需求场景如下: 一个公司,有N个员工,逢年过节时要搞一些抽奖活动,最终要公告收奖名单。 "员工"类如下: namespace CollectionBinding { /// //...
719 0

热门文章

最新文章