标题:如何使用依赖对象实现两个TextBox联动
只看楼主
hellenism
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2009-2-27
结帖率:76.47%
 问题点数:0 回复次数:2 
如何使用依赖对象实现两个TextBox联动
程序代码:
    public class Student : DependencyObject
    {
        public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Student));

        public string Name
        {
            get 
            {
                return (string)GetValue(NameProperty);
            }
            set
            {
                SetValue(NameProperty, value);
            }
        }

        public BindingExpressionBase SetBinding(DependencyProperty dp, BindingBase binding)
        {
            return BindingOperations.SetBinding(this, dp, binding);
        }
    }

    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();

            Student stu = new Student();
            stu.SetBinding(Student.NameProperty, new Binding("Text") { Source = this.textBox1});
            this.textBox2.SetBinding(TextBox.TextProperty, new Binding("Name") { Source = stu});
        }




请问textBox1与textBox2为什么没有实现联动呢?

[ 本帖最后由 hellenism 于 2011-4-27 18:18 编辑 ]
搜索更多相关主题的帖子: return 
2011-04-27 18:07
hellenism
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2009-2-27
得分:0 
请问为什么 textBox1和textBox2没有实现联动呢?
2011-04-27 18:17
雨夜星光
Rank: 4
等 级:业余侠客
帖 子:66
专家分:230
注 册:2009-11-25
得分:0 
不知楼主说的联动是什么样的联动?是一个txb中写的同时,另一个txb中也会随之显示吗?这样的话,直接可以用绑定的效果实现的啊。

C/C++交流群:65802335
2011-05-03 20:53



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-338146-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.438214 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved