简体   繁体   English

绑定两个文本框使用caliburn.micro c#wpf保持同步

[英]Binding two textbox keep synchronized using caliburn.micro c# wpf

I use Caliburn.Micro and WPF. 我使用Caliburn.Micro和WPF。 The View's name is : NewPlayView and the ViewModel's name is : NewPlayViewModel. View的名称是:NewPlayView和ViewModel的名称是:NewPlayViewModel。

I have two textbox, I would like that by changing the value of textbox1, the value of textbox2 various accordingly. 我有两个文本框,我想通过更改textbox1的值,textbox2的值相应各种。 For example, if I put 10 in textbox1, textbox2 displays in 20 (10 * 2). 例如,如果我在textbox1中放置10,则textbox2显示在20(10 * 2)中。

 <TextBox Height="22" HorizontalAlignment="Left" x:Name="textbox1"/>
 <TextBox Height="22" HorizontalAlignment="Left" x:Name="textbox2"/>

What i can do this ? 我能做到这一点? Thanks 谢谢

you either create 2 properties in your NewPlayViewModel model, lets say Value1 and Value2 and you bind them accordingly: 您要么在NewPlayViewModel模型中创建2个属性,请说Value1Value2 ,然后相应地绑定它们:

<TextBox Height="22" HorizontalAlignment="Left" x:Name="textbox1" Text="{Binding Path=Value1}"/>
<TextBox Height="22" HorizontalAlignment="Left" x:Name="textbox2" Text="{Binding Path=Value2}"/>

and when Value1 changes you recalculate Value2 (or vice versa) and notify UI that both values have changed Value1更改时,您重新计算Value2 (反之亦然)并通知 UI两个值都已更改

or you keep just one property, for example Value , create custom IValueConverter , implement both Convert and ConvertBack methods with your algorithm and bind it like this: 或者只保留一个属性,例如Value ,创建自定义IValueConverter ,使用您的算法实现ConvertConvertBack方法并将其绑定如下:

<TextBox Height="22" HorizontalAlignment="Left" x:Name="textbox1" Text="{Binding Path=Value}"/>
<TextBox Height="22" HorizontalAlignment="Left" x:Name="textbox2" Text="{Binding Path=Value, Converter={StaticResource MyValueConverter}}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM