應用一個Converter
如下面給你的綁定應該做的伎倆:
public class TextConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
int actual = (int)value;
return actual.ToString();
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
string actual = (string)value;
int theValue = 0;
int.TryParse(actual, out theValue);
return theValue;
}
}
你TextBox
結合會是這個樣子:
<TextBox Text="{Binding ... Converter={StaticResource convert}}"></TextBox>
隨着定義爲一個資源轉換器你的Window/Control/...