[英]C#/WPF Display Custom Strings (e.g. replace “0” with string.empty)
我已將TextBox綁定到字符串值
Text="{Binding AgeText, Mode=TwoWay}"
如何為字符串“0”顯示string.empty或“”,以及所有其他字符串的原始值?
謝謝你的幫助!
干杯
PS:一種方法是字符串的自定義ViewModel ..但我更願意直接在XAML中進行,如果可能的話。
我認為使用ViewModel的唯一方法是創建自定義ValueConverter。
所以基本上你的選擇是:
視圖模型:
private string ageText;
public string AgeText{
get{
if(ageText.equals("0"))
return string.empty;
return ageText;
}
...
}
ValueConverter:
public class AgeTextConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value.Equals("0"))
return string.Empty;
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
}
}
我在http://www.codeproject.com/KB/books/0735616485.aspx上找到了一些東西
這樣就可以了:
Text="{Binding AgeText, StringFormat='\{0:#0;(#0); }'}"
干杯
由於Age屬性在這里顯然是一個數字,另一種方法是將Age公開為int並使用Binding
的StringFormat
屬性:
Text="{Binding Age, Mode=TwoWay, StringFormat='{}{0:#}'}"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.