问题描述
嗨;有源文本框和目标文本框txttarget具有对txtsource的绑定.在txtsource中写东西时,txttarget改变了.一切都很好.但是在txttarget上书写时,我在txttarget上看不到任何变化吗?有双向模式.单向模式还不够吗?不用"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>
txtTarget.Text
每当绑定源(txtSource.Text
)更改时都会更新.
绑定模式为TwoWay
,这意味着对txtTarget.Text
的更改将反映到绑定的源.什么时候?这取决于Binding.UpdataSourceTrigger
属性.
如果您希望目标绑定在更改时更新源绑定,则必须使用Binding.UpdataSourceTrigger = OnPropertyChanged
,否则当txtTarget失去焦点时(默认行为),您将更新绑定源.
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>
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 useBinding.UpdataSourceTrigger = OnPropertyChanged
, otherwise you will update the binding source when txtTarget losts focus (default behavior).
这篇关于为什么我使用UpdateSourceTrigger = PropertyChanged,TwoWay还不够?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!