博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
7.创建动态绑定
阅读量:4477 次
发布时间:2019-06-08

本文共 1596 字,大约阅读时间需要 5 分钟。

适用于silverlight3、4、5

1. 新建一个项目,在MainPage.Xaml文件中添加控件,代码如下:

View Code

2. 新建一个类Myself.cs作为数据源

View Code
using System;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;namespace SilverlightApplication_DynamicBindings{public class MySelf{public string MyName { get; set; }public int MyAge { get; set; }public string MySex { get; set; }}}

3. 在MainPage.Xaml.cs 文件中,实例化类,并进行数据绑定

View Code
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;namespace SilverlightApplication_DynamicBindings{public partial class MainPage : UserControl{public MainPage(){InitializeComponent();//初始化Myself实例MySelf i = new MySelf();MyName = "小刀";MyAge = 24;MySex = "男";//在C#代码中创建绑定this.MyName.SetBinding(TextBlock.TextProperty, new System.Windows.Data.Binding("MyName"));this.MyAge.SetBinding(TextBlock.TextProperty, new System.Windows.Data.Binding("MyAge"));this.MySex.SetBinding(TextBlock.TextProperty, new System.Windows.Data.Binding("MySex"));//将Grid的上下文设置问Myself实例GridMyself.DataContext = i;}}}

 

运行效果如下:

总结:在这个例子中,textblock控件的Text属性是在C#代码中绑定的,实在程序运行过程中动态绑定的。

转载于:https://www.cnblogs.com/yuanjiedao/archive/2013/02/27/2934744.html

你可能感兴趣的文章
SparkStreaming入门及例子
查看>>
Web应用增加struts2支持
查看>>
java程序——凯撒加密
查看>>
Windows Store App之数据存储
查看>>
English class 82 The Importance of traveling
查看>>
python用递归函数解汉诺塔游戏
查看>>
Redis与Python交互
查看>>
Maximum-SubsequenceSum
查看>>
常用的一些shell变量
查看>>
Android无法删除项目+导入项目报错
查看>>
poj 2349(最小生成树应用)
查看>>
python接口自动化测试二十五:执行所有用例,并生成HTML测试报告
查看>>
c# 指定的存储区提供程序在配置中找不到,或者无效
查看>>
最简陋的python数据
查看>>
第一堂java web课
查看>>
操作系统简介
查看>>
第1周小组博客作业--1703班06组
查看>>
vue项目中icon图标的完美引入
查看>>
C语言指针
查看>>
Java的安装
查看>>