如何使用MVVM模式将十进制值绑定到wpf中的文本框? [英] How to bind decimal values to textbox in wpf using MVVM pattern?

查看:85
本文介绍了如何使用MVVM模式将十进制值绑定到wpf中的文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用MVVM模式创建wpf项目。

请告诉我如何使用mvvm来绑定十进制值和TextBox控件。

谢谢

解决方案

请参阅我对该问题的评论。您是否希望在每个UI和数据中看到绑定? :-)

无论你做什么,一切都归结为:

  decimal   value  =  //   ...  
MyTextBox.Text = value .ToString();
// ...
if decimal .TryParse(MyTextBox.Value, out value ))
// 使用值...: - )





-SA


设置Text属性使用ViewModel类中定义的公共属性。将十进制值设置为与TextBox Control绑定的属性。


请在此链接中找到解决方案

在文本框中绑定十进制值 [ ^ ]



您也可以使用以下字符串格式



 <   TextBox    文本  =  {Binding TransactionDetails.TransactionAmount,Mode = TwoWay,
UpdateSourceTrigger = PropertyChanged,StringFormat = N2}
Grid.Column = 3

Grid.ColumnSpan = 2 Grid.Row = 5 x:名称 = TextBoxAmount / >


Hi all,
I am creating wpf project using MVVM pattern.
Please tell me how can i bind decimal value with TextBox control using mvvm.?
Thankyou

解决方案

Please see my comment to the question. Do you want to see binding in each and every piece of UI and data? :-)
No matter what you do, everything boils down to this:

decimal value = //...
MyTextBox.Text = value.ToString();
// ...
if (decimal.TryParse(MyTextBox.Value, out value))
   // use value... :-)



—SA


Set the Text property with a public property defined in ViewModel class. set the decimal value to that property binded with TextBox Control.


Please find the solution in this link
Bind Decimal Value in textbox[^]

also you can use string format as follows

<TextBox Text="{Binding TransactionDetails.TransactionAmount, Mode=TwoWay, 
UpdateSourceTrigger=PropertyChanged,StringFormat=N2}" Grid.Column="3" 

Grid.ColumnSpan="2" Grid.Row="5" x:Name="TextBoxAmount" />


这篇关于如何使用MVVM模式将十进制值绑定到wpf中的文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆