Why am i using UpdateSourceTrigger=PropertyChanged ,TwoWay is not enough?

对着背影说爱祢 提交于 2019-12-21 04:11:30

问题


hi; there are Source and target textbox txttarget has a binding to txtsource. when writing something in txtsource, txttarget is changed.Everything is good. But writing on txttarget, i dont see any changes at txttarget? there is TwoWay mode. Twoway mode is not enough? can i write without using "UpdateSourceTrigger=PropertyChanged"?


   <Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="155,62,0,0" Name="txtSource" VerticalAlignment="Top" Width="120" />
        <TextBox Height="23" HorizontalAlignment="Left"
                 Text="{Binding ElementName=txtSource,Path=Text,Mode=TwoWay}" 
                 Margin="155,113,0,0" Name="txtTarget" VerticalAlignment="Top" Width="120" />
    </Grid>

回答1:


txtTarget.Text is updated whenever the bound source (txtSource.Text) changes.

The binding mode is TwoWay which means that changes to txtTarget.Text will be reflected to the bound source. When? It depends on the Binding.UpdataSourceTrigger property.

If you want your target binding to update your source binding when changing you must use Binding.UpdataSourceTrigger = OnPropertyChanged, otherwise you will update the binding source when txtTarget losts focus (default behavior).




回答2:


The default UpdateSourceTrigger for a TextBox is LostFocus (see Binding.UpdateSourceTrigger). If you do not specify PropertyChanged as the UpdateSourceTrigger, what you type into txtTarget will not be written to txtSource until txtTarget loses focus (that is you tab off of it).



来源:https://stackoverflow.com/questions/13601101/why-am-i-using-updatesourcetrigger-propertychanged-twoway-is-not-enough

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!