using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Data; namespace WpfApp2 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { InitializeComponent(); //实例化绑定对象 Binding textBinding = new Binding(); //设置要绑定源 textBinding.Source = this;//绑定MainWindow类 //设置要绑定属性 textBinding.Path = new PropertyPath("TxtValue");//绑定MainWindow类下的TxtValue属性。 textBinding.Mode = BindingMode.TwoWay;//绑定模式双向绑定 //设置绑定到要绑定的控件 this.myTb.SetBinding(TextBlock.TextProperty, textBinding); } private string _text = "Text"; public string TxtValue { get { return _text; } set { _text = value; OnPropertyChanged("TxtValue"); } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string info) { var handler = PropertyChanged; handler?.Invoke(this, new PropertyChangedEventArgs(info)); } } }
<Window x:Class="WpfApp2.MainWindow" 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:WpfApp2" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <TextBlock x:Name="myTb"/> </Grid> </Window>
生成结果如下
原文地址:http://www.cnblogs.com/lizhiqiang0204/p/16798241.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性