首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > CAD教程 >

为啥依赖属性在五秒钟内起作用

2013-11-25 
为什么依赖属性在五秒钟内起作用。出现的问题:在五秒钟内改变textbox的值,lable 和textblock都可以改变,过

为什么依赖属性在五秒钟内起作用。
出现的问题:在五秒钟内改变textbox的值,lable 和textblock都可以改变,过了五种就不行了。

namespace WpfApplication1
{

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Student stu = new Student();
            stu.SetBinding(Student.NameProperty, new Binding("Text") { Source = tx1 });//数据源为 textbox
            l1.SetBinding(Label.ContentProperty, new Binding("Name") { Source = stu }); //l1为lable
            txb1.SetBinding(TextBlock.TextProperty, new Binding("Name") { Source = stu });//txb1为TextBlock
            
            
               
        }
    }

    public class Student:DependencyObject
    {


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


        public static readonly DependencyProperty NameProperty =
            DependencyProperty.Register("Name", typeof(string), typeof(Student));

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

        
        
    }
}
[解决办法]
把 Student stu = new Student(); 放到外面(MainWindow的全局变量)。

热点排行