我如何为绑定(bind)添加格式,用 string.Format 或类似的东西格式化绑定(bind)值?我在其他线程中看到,您可以传递 converterName。
- 是否存在针对此问题的转换器?
- 在哪里可以看到 MvvMCross v3 的标准转换器列表?
我很快浏览了代码,但找不到任何东西。我知道可能会丢失信息,这会破坏双向绑定(bind),但我只想用它来显示值。 我的具体案例是 DateTime 的绑定(bind)。
bindings.Bind(purchaseDate).To(vm => vm.RegisteredDevice.PurchaseDate);
我的愿望例如:
bindings.Bind(purchaseDate).To(vm => vm.RegisteredDevice.PurchaseDate).WithFormat("hh:mm");
最佳答案
为此,您只需创建一个 StringFormatValueConverter 并将其参数用作要使用的格式字符串。
应该需要大约 2 分钟来写...在这里,我会证明它:
public class StringFormatValueConverter : MvxValueConverter
{
public override object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return null;
if (parameter == null)
return value;
var format = "{0:" + parameter.ToString() + "}";
return string.Format(format, value);
}
}
然后
set.Bind(myLabel).To(vm => vm.TheDate).WithConversion("StringFormat", "HH:MM:ss");
1 分 53 秒 ;)
关于c# - 带格式字符串的 MvvMCross 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16398341/