3
A
回答
1
您可以在您的WCF DataContract中实现[OnSerializing]事件来检查String.Empty并将其更改为null。 EG:
[DataContract]
class MyDataStructure
{
[DataMember]
string Foo { get; set; }
[OnSerializing]
void OnSerializing(StreamingContext context)
{
if (Foo == String.Empty)
Foo = null;
}
}
如果你有很多字符串属性的,不希望编写代码来测试每一个,你总是可以通过类的属性,使用反射来循环。
2
可以使用的IValueConverter类像下面::下面的代码是双层,可以激发的行为对其他类型
public class DoubleNullFromStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is double || value is double?) return value;
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return null;
var strValue = value as string;
if (strValue == null || strValue.Trim().Length == 0)
return null; //allow empty strings and whitespaces
double doubleValue = 0;
if (double.TryParse(strValue, out doubleValue))
return doubleValue;
return value; //which will intenionally throw an error
}
}
然后绑定这对你的控制像下面
<TextBox HorizontalAlignment="Left" Name="brokeragePaidText" VerticalAlignment="Top" Width="170" >
<TextBox.Text>
<Binding Source="{StaticResource insertTransaction}" Converter="{StaticResource DoubleNullFromStringConverter}" UpdateSourceTrigger="Explicit" Path="BrokeragePaid">
<Binding.ValidationRules>
<ExceptionValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
相关问题
- 1. Symfony:保存null而不是空字符串
- 2. Rails f.select仅保存空白字符串而不是选项值
- 3. 如何获得一个TDataset来存储空字符串而不是null?
- 4. 从空白处传递NULL /空字符串到Oracle存储过程
- 5. NSPredicate测试NULL,并空白字符串
- 6. Oracle 10g:列是空白的,但不为空或空字符串
- 7. NHibernate应该总是写NULL而不是空字符串
- 8. Ajax返回的是空字符串而不是NULL值
- 9. NSLog返回null而不是字符串
- 10. StringTokenizer返回null而不是字符串
- 11. MySQL和PHP - 插入NULL而不是空字符串
- 12. 如何在PHP PDO中插入空字符串而不是null
- 13. JAXB返回null而不是空字符串
- 14. 希望能够接收空字符串而不是null
- 15. 如何让Tomcat输出空字符串而不是null?
- 16. 为什么这个BufferedReader返回空字符串而不是null?
- 17. Golang将NULL插入到SQL中而不是空字符串
- 18. Kohana的ORM插入NULL而不是空字符串
- 19. 空字符串+ null =“null”?
- 20. .Net:空字符串不是空白字符?
- 21. ActiveRecord + ActiveAdmin,保存一个空字符串而不是空
- 22. 在MySQL列中匹配空字符串而不是空值
- 23. Redis如何存储一个数字而不是字符串
- 24. Gedmo/Sluggable存储数字而不是字符串
- 25. 空字符串返回而不是IndexError
- 26. 零返回,而不是空字符串
- 27. PHP转换NULL在MSSQL空字符串存储过程
- 28. Oracle ResultSet.getString(“colname”)返回字符串“null”而不是java null
- 29. SSRS报告显示NULL值的文本框中的空白但我想要一个空字符串而不是
- 30. ComboBox.Text =空字符串,而不是实际显示的字符串