TextBox的Text属性更新问题

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zhaogengzi/article/details/36657493

对TextBox使用数据绑定时,如果TextBox的Text属性值发生改变,只有在TextBox失去焦点时,Text的值才会被更新到数据源,这种方式有一些情况下并不适用,我需要Text值的改变能实时反映到数据源.

       要修改更新模式,可以在绑定时设置

<TextBlock Text="{Binding UserName,  UpdateSourceTrigger= Explicit}"/>
      在windows phone 的silverlight中 UpdateSourceTrigger只有两个值,一个是:Default,另一个是Explicit.数据绑定时,默认情况下就是Default,指的是在TextBox失去焦点时,Text值会更新到数据源,如果设置成Explicit,需要手工调用一下

BindingExpression.UpdateSource(),这个Text值会更新到数据源.

   

// Summary:
    //     Defines constants that indicate when a binding source is updated by its binding
    //     target in two-way binding.
    public enum UpdateSourceTrigger
    {
        // Summary:
        //     The binding source is updated automatically when the binding target value
        //     changes.
        Default = 0,
        //
        // Summary:
        //     The binding source is updated only when you call the System.Windows.Data.BindingExpression.UpdateSource()
        //     method.
        Explicit = 3,
    }
  基本思路是利用TextBox的TextChanged事件去调用UpdateSource()

  

  <TextBox Text="{Binding UserName,  UpdateSourceTrigger= Explicit}" 
                      TextChanged="TextBox_TextChanged_1"/>

 private void TextBox_TextChanged_1(object sender, TextChangedEventArgs e)
        {
            TextBox tb = sender as TextBox;
            BindingExpression be = tb.GetBindingExpression(TextBox.TextProperty);
            be.UpdateSource();
        }
    这个效果就实现了...

    可以把这个段码用behaviour封装一下.这样复用性就强了.


展开阅读全文

新手请教TextBoxText属性绑定问题

01-20

我是个C#新手,最近有个程序要把TextBox的Text属性与一个int属性绑定,发现一个奇怪的事,在一个属性中更改另一个属性,TextBox控件显示的值就可以自动更新,但用点击按钮的方法就无效。请看代码:rnrn用于演示的类Class1:rn[code=C#]using System;rnusing System.Collections.Generic;rnusing System.Linq;rnusing System.Text;rnrnnamespace WindowsFormsApplication3rnrn class Class1rn rn private int _a = 1;rnrn public int Arn rn get return _a; rn set rn rnrn if (_a != value)rn rn _b = value + 1;rn _a = value; rn rn rn rnrn private int _b = 2;rnrn public int Brn rn get return _b; rn set _b = value; rn rn rnrn[/code]rn当A的值改变时,B变为A+1。rnrn界面:rn[code=C#]using System;rnusing System.Collections.Generic;rnusing System.ComponentModel;rnusing System.Data;rnusing System.Drawing;rnusing System.Linq;rnusing System.Text;rnusing System.Windows.Forms;rnrnnamespace WindowsFormsApplication3rnrn public partial class Form1 : Formrn rn private Class1 _class1 = new Class1();rn private TextBox tbA;rn private TextBox tbB;rn private Button button1;rnrn public Form1()rn rn tbA = new TextBox(); //与A绑定rn tbB = new TextBox(); //与B绑定rn button1 = new Button();rn rn tbA.Location = new Point(105, 39);rn tbA.Name = "tbA";rn tbA.Size = new Size(121, 21);rn rn tbB.Location = new Point(106, 118);rn tbB.Name = "tbB";rn tbB.Size = new Size(119, 21);rn rn button1.Location = new Point(148, 67);rn button1.Name = "button1";rn button1.Size = new Size(23, 45);rn button1.TabIndex = 2;rn button1.Text = "B加1";rn button1.Click += new System.EventHandler(button1_Click);rnrn this.Controls.Add(this.button1);rn this.Controls.Add(this.tbB);rn this.Controls.Add(this.tbA);rnrn tbA.DataBindings.Add("Text", _class1, "A");rn tbB.DataBindings.Add("Text", _class1, "B");rn rnrn private void button1_Click(object sender, EventArgs e)rn rn _class1.B += 1;rn rn rnrn[/code]rn在tbA中改变数值后按tab,tbB中的数值会自动更新。但点button1后B的值实际上是改变的,但控件中不会更新,除非用INotifyPropertyChanged。请问原因在哪里? 论坛

没有更多推荐了,返回首页