wpf 为什么我使用 UpdateSourceTrigger=PropertyChanged ,TwoWay 是不够的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13601101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:23:35  来源:igfitidea点击:

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

c#.netwpfxamlpropertychanged

提问by loki

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"?

你好; 有源和目标文本框 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>

回答by Klaus78

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

txtTarget.Text每当绑定源 ( txtSource.Text) 更改时更新。

The binding mode is TwoWaywhich means that changes to txtTarget.Textwill be reflected to the bound source. When? It depends on the Binding.UpdataSourceTriggerproperty.

绑定模式TwoWay意味着更改txtTarget.Text将反映到绑定源。什么时候?这取决于Binding.UpdataSourceTrigger财产。

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).

如果您希望目标绑定在更改时更新源绑定Binding.UpdataSourceTrigger = OnPropertyChanged,则必须使用 ,否则您将在 txtTarget 失去焦点时更新绑定源(默认行为)。

回答by shf301

The default UpdateSourceTriggerfor a TextBoxis LostFocus(see Binding.UpdateSourceTrigger). If you do not specify PropertyChangedas the UpdateSourceTrigger, what you type into txtTargetwill not be written to txtSourceuntil txtTargetloses focus (that is you tab off of it).

默认UpdateSourceTriggerTextBoxLostFocus(见Binding.UpdateSourceTrigger)。如果您没有指定PropertyChanged为 UpdateSourceTrigger,则您输入的内容txtTarget将不会被写入,txtSource直到txtTarget失去焦点(即您将其关闭)。