WPF属性绑定实现双向变化
WPF依赖项属性可以实现属性的绑定,成功绑定之后只要修改后台绑定的属性,即可UI同步自动更新绑定的值,无需手动刷新界面;同样,前台的值变化后,通过获取绑定的属性值也可获取UI变化后的值,实现双向变化的效果。属性绑定使得UI更新非常的方便,下面分享一个小栗子说明使用的方式。
1、先做了一个有一个TextBlock和一个Button的UI,想要实现点击后TextBlock发生变化。
<Window x:Class="WPFDemo.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFDemo"
mc:Ignorable="d"
Title="Window1" Height="" Width=""> <Grid>
<Button Name="Button_OK" MaxWidth="" MaxHeight="" Click="Button_OK_Click">OK</Button>
<TextBlock MaxWidth="" MaxHeight="" VerticalAlignment="Top" HorizontalAlignment="Left" Text="{Binding Text}"></TextBlock>
</Grid>
</Window>
2、创建UI更新类(现在是测试,所以属性比较少,正常开发建议一个UI创建一个UI更新类专门用于UI更新),如下为完整代码
public class PropertyToUI : INotifyPropertyChanged
{
#region 私有变量 /// <summary>
/// 状态栏显示文本
/// </summary>
private string text = ""; #endregion #region 属性 /// <summary>
/// 属性-显示文本
/// </summary>
public string Text
{
get { return text; }
set
{
text = value;
OnPropertyChanged("Text");
}
} #endregion #region 属性变化通知事件 /// <summary>
/// 属性变化通知事件
/// </summary>
public event PropertyChangedEventHandler PropertyChanged; /// <summary>
/// 属性变化通知
/// </summary>
/// <param name="e"></param>
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
} /// <summary>
/// 属性变化通知事件
/// </summary>
/// <param name="PropertyName"></param>
public void OnPropertyChanged(string PropertyName)
{
PropertyChangedEventArgs e = new PropertyChangedEventArgs(PropertyName);
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
} #endregion
}
这个部分有如下几个关键点:
(1)、需要实现INotifyPropertyChanged接口,这是一个属性更新接口,可以看一下它的实现,有一个属性更新事件,所以要说声明该事件。
namespace System.ComponentModel
{
//
// 摘要:
// Notifies clients that a property value has changed.
public interface INotifyPropertyChanged
{
//
// 摘要:
// Occurs when a property value changes.
event PropertyChangedEventHandler PropertyChanged;
}
}
(2)、创建属性更新函数
/// <summary>
/// 属性变化通知
/// </summary>
/// <param name="e"></param>
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
参数为某个属性的更新事件,而后触发PropertyChanged(this, e)通知UI更新指定属性
(3)、包装属性
public string Text
{
get { return text; }
set
{
text = value;
OnPropertyChanged("Text");
}
}
在设置器中调用属性更新事件,即当后台设置值时(想要更新UI值),就会触发属性更新事件,通知前台绑定的依赖项属性进行更新(事件中带有属性的身份标识和值进行传递)。
3、前台依赖项属性对属性更新类中的属性进行绑定(Binding语法)
<TextBlock MaxWidth="" MaxHeight="" VerticalAlignment="Top" HorizontalAlignment="Left" Text="{Binding Text}"></TextBlock>
属性名绑定即可
4、绑定数据源的说明(这是比较容易忘记的地方)
PropertyToUI UI = new PropertyToUI();
this.DataContext = UI; //事件绑定数据源
以上就是属性绑定的必要步骤了,如果没什么问题基本就成功了,没成功的再好好检查一下。
如下为完整的后台代码:
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{ /// <summary>
/// UI更新类对象
/// </summary>
PropertyToUI UI = new PropertyToUI(); /// <summary>
/// 构造函数
/// </summary>
public Window1()
{
InitializeComponent(); this.DataContext = UI; //事件绑定数据源 UI.Text = "程序开启";
} /// <summary>
/// OK按键点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_OK_Click(object sender, RoutedEventArgs e)
{
UI.Text = "我更新了";
MessageBox.Show(UI.Text);
} }
运行效果如下:

点击OK按键后:

WPF属性绑定实现双向变化的更多相关文章
- 2-4 Vue中的属性绑定和双向数据绑定
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Vue学习之vue属性绑定和双向数据绑定
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vuejs属性绑定和双向绑定
属性绑定 html <div v-bind:title="title">hello world</div> js new Vue({ el:'#root', ...
- WPF使用MVVM(一)-属性绑定
WPF使用MVVM(一)-属性绑定 简单介绍MVVM MVVM是Model(数据类型),View(界面),ViewModel(数据与界面之间的桥梁)的缩写,是一种编程模式,优点一劳永逸,初步增加一些逻 ...
- 【WPF】如何把一个枚举属性绑定到多个RadioButton
一.说明 很多时候,我们要把一个枚举的属性的绑定到一组RadioButton上.大家都知道是使用IValueConverter来做,但到底怎么做才好? 而且多个RadioButton的Checked和 ...
- 学习WPF——元素绑定
概念 从源对象提取一些信息,并用这些信息设置目标对象的属性 示例 在给TextBlock控件的FontSize属性赋值时,我们使用了绑定表达式 数据绑定表达式使用XAML的标记扩展(因此具有花括号)( ...
- WPF - 属性系统 (3 of 4)
依赖项属性元数据 在前面的章节中,我们已经介绍了WPF依赖项属性元数据中的两个组成:CoerceValueCallback回调以及PropertyChangedCallback.而在本节中,我们将对其 ...
- 控制文本和外观------Attr Binding(attr属性绑定)
Attr Binding(attr属性绑定) 目的 attr 绑定提供了一种方式可以设置DOM元素的任何属性值.你可以设置img的src属性,连接的href属性.使用绑定,当模型属性改变的时候,它会自 ...
- 【Angular 5】数据绑定、事件绑定和双向绑定
本文为Angular5的学习笔记,IDE使用Visual Studio Code,内容是关于数据绑定,包括Property Binding.Class Binding.Style Binding. 在 ...
随机推荐
- koa2跨域模块koa2-cors
之前写了一个api在小程序里调用,但是我不想每次都打开小程序,所以想写一个简单的网页,但是遇到CORB的问题: 经尝试,jsonp等都没起作用,由于我后台是koa写的,发现koa2-cors库可以解决 ...
- redis(二)集群 redis-cluster & redis主从同步
参考文档: http://geek.csdn.net/news/detail/200023 redis主从复制:https://blog.csdn.net/imxiangzi/article/deta ...
- Yii2 中常用的增删改查操作总结
一.新增 1.使用save() $model = new User(); $model->name = 'test'; $model->phone = '13000000000'; $mo ...
- css流星 效果
style: .loding { width: 100%; height: 100%; } .bg{ width: 100%; height: 100%; ...
- 你的首个golang语言详细入门教程 | your first golang tutorial
本文首发于个人博客https://kezunlin.me/post/a0fb7f06/,欢迎阅读最新内容! your first golang tutorial go tutorial version ...
- FCC---Create a Gradual CSS Linear Gradient
Applied Visual Design: Create a Gradual CSS Linear Gradient background: linear-gradient(gradient_dir ...
- AI-Web1靶机渗透
先上一张图: 靶机信息及地址:https://www.vulnhub.com/entry/ai-web-1,353/ 下载到靶机后,在VMware里打开,kali使用arp-scan -l 扫到 在浏 ...
- CodeForces - 1238D(思维)
题意 https://vjudge.net/problem/CodeForces-1238D 如果一个字符串的每个字母,属于至少一个(长度大于1)的回文串,则称这个字符串为good. 一个长度为n的字 ...
- 科研画图:散点连接并平滑(基于Matlab和Python)
导师要求参照别人论文中的图(下图),将其论文中的图画美观些,网上关于科研画图相关的代码比较少,就自己鼓捣了下. 附上自己整合验证过的代码: 功能:将散点连接并平滑 1)Matlab 效果图: x1=[ ...
- vue模板语法下
样式绑定 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...