Reputation: 6397
I've bound my TextBox to a string value via
Text="{Binding AgeText, Mode=TwoWay}"
How can I display string.empty or "" for the string "0", and all other strings with their original value?
Thanks for any help!
Cheers
PS: One way would be a custom ViewModel for the string.. but I'd prefer to do it somehow in the XAML directly, if it's possible.
Upvotes: 6
Views: 7594
Reputation: 13025
Since the Age property is obviously a number here, an other way to go would be to expose the Age as an int and use the StringFormat
attribute of the Binding
:
Text="{Binding Age, Mode=TwoWay, StringFormat='{}{0:#}'}"
Upvotes: 4
Reputation: 6397
I found something on http://www.codeproject.com/KB/books/0735616485.aspx
This will do the trick:
Text="{Binding AgeText, StringFormat='\{0:#0;(#0); }'}"
Cheers
Upvotes: 7
Reputation: 3127
I think the only way beside using the ViewModel is creating a custom ValueConverter.
So basically your choices are:
ViewModel:
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)
{
}
}
Upvotes: 7