Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HTML
<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
Posted

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.
 
Share this answer
 
v2
Comments
jinxinhelloworld 25-Sep-15 11:44am    
Boss,Thanks for your reply on a very late night.
Maybe I did not express me clear, that you did not answer the question.

I mean, my expectation is that ,only After key in "ABCDEFG" and key in Tab to make the txt1 lostfocus, so the BindngSource will be updated, and then the txt2 will display "D"

But actually After I key in "ABCD" with out key in Tab, the txt2 can display "D"

Because as I know, the default UpdateSourceTrigger of TextBox is LostFocus.
Afzaal Ahmad Zeeshan 25-Sep-15 12:20pm    
You have not shown where that property is being set, but as the code you can see. There is only the Text[3] part in your XAML, which gets triggered if there is a value.

I am not sure whether that should throw exception or override it (as in your case) but what happens is that until Text[3] targets a valid character it does not show anything on screen. When it does, it prints "D" in the TextBlock.

Can you also show the code-behind?

Remember: Default value for UpdateSourceTrigger is PropertyChanged that is why when you change the property it would trigger. :-) I hope this is the answer.
jinxinhelloworld 25-Sep-15 12:23pm    
Hi boss most of control is PropertyChanged, but Textbox is LostFocus.
And there is no backend code required.
https://msdn.microsoft.com/en-us/library/system.windows.data.binding.updatesourcetrigger%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
Afzaal Ahmad Zeeshan 25-Sep-15 12:34pm    
Not sure why there is an ambiguity in that. But it works as if that was set to <code.propertychanged< code="">. I tried it in my environment and same results, also no change if you try to change the <code>Mode too.

If you want to update the value only when Tab key is pressed or when the control loses focus. You should consider handling the LostFocus event and then get the value.
jinxinhelloworld 25-Sep-15 18:50pm    
No need handle the LostFocus event.
It is another value of UpdateSourceTrigger
No need handle the LostFocus event.
It is another value of UpdateSourceTrigger
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900