简体   繁体   English

在Windows Phone应用程序中的MVVM中格式化数据

[英]Formatting Data in MVVM within Windows Phone app

I have a DataModel and a ViewModel working successfully together with my xaml view. 我有一个DataModel和ViewModel可以与我的xaml视图一起成功地工作。 In the view I use databindings. 在视图中,我使用数据绑定。

In my DataModel I have some properties like Distance declared as int . 在我的DataModel中,我有一些属性,例如Distance声明为int

When displaying the values in view, I want a formatting like adding a trailing meter . 在视图中显示值时,我想要一种格式,例如添加尾随meter

How get this done? 如何做到这一点?

您可以在xaml绑定中格式化字符串...

<TextBlock Text="{Binding Distance, StringFormat={} {0} meter}"/>

Maybe by providing a property which will return a formatted value: 也许通过提供一个将返回格式化值的属性:

private int distance = 0;
public int Distance
{
   get { return distance;}
   set { distance = value; OnPropertyChanged("DistanceTxt"); }
}

public string DistanceTxt
{
   get { return distance.ToString() + " meter"; }
}

Then when you bind to DistanceTxt you should get your distance with trailing meter . 然后,当您绑定到DistanceTxt时,您应该使用尾随meter获取distance I've also added OnPropertyChanged in Distance property so that when it changes, your value on the screen updates. 我还在Distance属性中添加了OnPropertyChanged ,以便当它更改时,屏幕上的值也会更新。

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

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