什么是TextBox的UpdateSourceTrigger [英] what is UpdateSourceTrigger of TextBox

查看:61
本文介绍了什么是TextBox的UpdateSourceTrigger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <  窗口    x:Class   =  C06P90 .MainWindow  

< span class =code-attribute> xmlns = http:// schemas.microsoft.com/winfx/2006/xaml/presentation\"

< span class =code-attribute> xmlns:x = http://schemas.microsoft.com/winfx/2006/xaml

标题 = MainWindow < span class =code-attribute>高度 = 350 宽度 = 525 >
< StackPanel >
< TextBox x:名称 = txt1 < span class =code-attribute>保证金 = 5 / >
< TextBox x:名称 = txt2 Text = {Binding Path = Text [3],ElementName = txt1,Mode = OneWay} 保证金 = 5 / >
< / StackPanel >
< / Window >







对于上面的代码,当我在txt1中键入ABCDEFG时,什么会发生吗?

我预期的结果是,在我键入ABCDEFG并键入Tab后,第4个字符将显示在t中他是txt2。



但实际上,在我输入ABCD之后,D将显示在txt2中。



任何人都可以告诉我原因?

在我看来,TextBox的UpdateSourceTrigger是Lostfocus

解决方案

使用此属性当您在应用程序中使用Binding时。您正在使用Binding,即可见。但你也许没有得到它。因此,这设置了Binding的工作原理。绑定是一个双向过程,在这两种方式中,您可以确定地选择绑定数据的位置。



1.双向:数据双向同步。

2.单向(来源):在这种方法中,编辑后的数据会丢失,除非您将其保存在其他地方。

3.单向(来自控制):不访问数据,但用户写入的内容存储在源中。



但是,你没有使用该属性 [ ^ 一点都不。你有吗?



现在回答你的问题,如果你只输入 ABCDEFG ,< b> D 将会出现。为什么?看看你的代码,



 Text ={Binding Path = Text [3],ElementName = txt1,Mode = OneWay} 





您正在使用 Text [3] ,字符串是一个字符数组所以它会返回数组中的第4个元素(你知道 string.Length string [index] 吗?)。这就是为什么,它总是写第4个元素。更改该Path值以获取不同的值。 :-)



此属性的默认值为 PropertyChanged 。当该控件的属性发生变化时会触发,您对Text属性感兴趣。所以它不会(永远!)等到你按Tab键。当该位置有一个有效字符时,它只会在第四个位置(索引3)写入字符。这就是为什么你的应用程序也不等你按Tab键。


不需要处理LostFocus事件。

它是UpdateSourceTrigger的另一个值

<Window x:Class="C06P90.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBox x:Name="txt1" Margin="5"/>
        <TextBox x:Name="txt2" Text="{Binding Path=Text[3],ElementName=txt1,Mode=OneWay}" Margin="5"/>
    </StackPanel>
</Window>




For the code above, When I keyed in "ABCDEFG" in txt1, What will happen?
The result I expected is that, after I keyed in "ABCDEFG" and key in Tab, then the 4th character will display in the txt2.

But actually, after I key in "ABCD" the "D" will display in the txt2.

Anyone can tell me why?
In my opinion, the UpdateSourceTrigger of TextBox is Lostfocus

解决方案

This property is used when you are making use of Binding in your application. You are using Binding, that is seen. But you didn't get it perhaps. So, this sets up how does Binding works. Binding is a two-way process, in these two way processes you can surely select where to bind your data.

1. Two-way: Data is synchronized both way.
2. One-way (from source): In this method, the data once edited is lost unless you save it somewhere else.
3. One-way (from control): Data is not accessed, but what user writes is stored in the source.

But, you didn't make use of that property[^] at all. Did you?

Now the answer to your question, if you enter "ABCDEFG" only, "D" will be presented. Why? Have a look at your code,

Text="{Binding Path=Text[3],ElementName=txt1,Mode=OneWay}"



You are using Text[3], string is an array of characters so it would return the 4th element in array (are you aware of string.Length and string[index]?). That is why, it would always write the 4th element. Change that Path value to get a different value. :-)

The default value for this property is PropertyChanged. Which would trigger when a property for that control would change, you are interested in Text property. So it would not (never!) wait until you press Tab. It would simply write the character at 4th location (index 3) when there is a valid character at that location. That is why your application also does not wait for you to press Tab.


No need handle the LostFocus event.
It is another value of UpdateSourceTrigger


这篇关于什么是TextBox的UpdateSourceTrigger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆