惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Schneier on Security
N
News and Events Feed by Topic
量子位
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
Hugging Face - Blog
Hugging Face - Blog
S
Security Affairs
J
Java Code Geeks
Schneier on Security
Schneier on Security
Google Online Security Blog
Google Online Security Blog
TaoSecurity Blog
TaoSecurity Blog
小众软件
小众软件
S
SegmentFault 最新的问题
www.infosecurity-magazine.com
www.infosecurity-magazine.com
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Security Archives - TechRepublic
Security Archives - TechRepublic
P
Privacy International News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
博客园 - 聂微东
T
Tor Project blog
博客园 - Franky
C
CERT Recently Published Vulnerability Notes
Cyberwarzone
Cyberwarzone
罗磊的独立博客
博客园_首页
The Cloudflare Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
大猫的无限游戏
大猫的无限游戏
Forbes - Security
Forbes - Security
V
Vulnerabilities – Threatpost
Security Latest
Security Latest
腾讯CDC
Simon Willison's Weblog
Simon Willison's Weblog
S
Securelist
博客园 - 【当耐特】
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threat Research - Cisco Blogs
博客园 - 司徒正美
AWS News Blog
AWS News Blog
WordPress大学
WordPress大学
Jina AI
Jina AI
G
GRAHAM CLULEY
V
V2EX
L
LINUX DO - 最新话题
H
Heimdal Security Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
IT之家
IT之家

博客园 - 挑战

从存储过程中读取相关信息 Blend Step by Step书籍笔记(第一章) WPF非轮询方式实时更新数据库变化SqlDependency 动态解析XAML文本构建WPF的UI 解决为'*********' 的游标已存在问题 数据表死锁查询和处理 SQL Server操作XML(六)XML FLOWR SQL Server操作XML(五)XML Query-XQuery SQL Server操作XML(四)XML数据类型 SQL Server操作XML(三)OPENXML函数功能 SQL Server操作XML(二)XML子句实例 SQL Server操作XML(一)XML子句 数据绑定 最为详尽的WPF类继承关系 LinQ数据访问 WPF Diagram Designer Part 3:连接Item 照猫画虎WPF之一:命名空间 解决WPF部署后客户端访问安全性问题 C#读取文本播放相应语音
照猫画虎WPF之二数据绑定
挑战 · 2012-03-12 · via 博客园 - 挑战

    数据绑定简而言之就是通过绑定数据源的形式,通过控件将数据展示出来。通过相关设置,使控件实现数据驱动,即当数据发生变化时,UI层展示数据同步变化。

包括:

(1)Source,数据源为后台自定义的数据类,要求绑定的属性为公有。

例如,声明数据源实体类(声明一个变量类,包括有压力、温度等属性,这里仅仅包含温度的实现)

namespace DataBinding
{
    public class Variable    

     {
        private int temperature;
        public int Temperature
        {
            get { return temperature; }
            set{ temperature = value; }
        }

    }
}

通过后台代码绑定显示

        public Page1()
        {
            InitializeComponent();
            Variable v = new Variable();
            v.Temperature = 10;                                               //初始温度为10°
            Binding binding = new Binding();                              //新建一个绑定对象

            binding.Source = v;                                                //指定绑定对象的数据源
            binding.Path = new PropertyPath("Temperature");      //指定绑定对象的对应属性
            BindingOperations.SetBinding(this.progressBar1, ProgressBar.ValueProperty, binding);      //将控件的相应属性与绑定对象关联
        }

 此时可以实现绑定展示数据源的属性值

 

     现在增加功能,加温或降温(温度值有可能内存动态修改,有可能底层数据库中修改,可以利用ServiceBroker实现,另外介绍),客户端实时展现底层数据变化,实现监控功能。

namespace DataBinding
{
    public class Variable : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged; // 这个接口仅包含一个事件而已  

        private int temperature;
        public int Temperature
        {
            get { return temperature; }
            set
            {
                temperature = value;
                if (this.PropertyChanged != null)                                  
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Temperature")); // 通知UI“Temperature”属性值改变   
                }
            }
        }

    }
}

通过手动修改temperature的方式,模拟加温和降温

       private void button1_Click(object sender, RoutedEventArgs e)
        {
            v.Temperature += 10;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            v.Temperature -= 10;
        }

 此时,可以通过数据绑定动态展现底层数据了

源代码

(2)ElementName,数据源为控件的依赖属性(可以看成是Source的特例,其数据源类型只能是控件),例如TextBox的字号,绑定到Slider的Value属性

 <Label Content="控件元素绑定" FontSize="{Binding ElementName=slider1, Path=Value}" Name="label1"  />    通过标记扩展方式实现

或者

 <Label Content="控件元素绑定"  Name="label1">         通过元素属性方式实现,优点是具有智能感知,而扩展方式没有智能感知
            <Label.FontSize>
                <Binding ElementName="slider1" Path="Value"  Mode="OneWay" />
            </Label.FontSize>
 </Label>