【发布时间】:2012-01-08 12:07:17
【问题描述】:
我正在尝试使用列表框来显示是/否并绑定到我的 Sql Server 数据库中的位字段。
我的代码(尤其是我的绑定)看起来正确吗?
当使用它时,它可以使用是(真)语句或否(假)语句创建记录,我什至可以将数据库中的否(假)更新为是(真)......但我无法将 Yes (true) 编辑为 No (False)...如果我调试一切看起来都不错,直到我点击我的 RiaService,然后使用 Sql Profiler,该字段不会更新。
哦,是的,如果我将相同的代码绑定到 varchar 字段并排除转换器,一切正常。
public class ConvertBoolToYesNo : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return null;
string outValue;
bool inValue = (bool)value;
outValue = inValue ? "Yes" : "No";
return outValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return null;
bool? outValue;
string inValue = (string)value;
inValue = inValue.Trim();
if (inValue == "Yes")
{
outValue = true;
}
else
if (inValue == "No")
{
outValue = false;
}
else
{
return DependencyProperty.UnsetValue;
}
return outValue;
}
<ListBox x:Name="lbCounselExempt"
ItemsSource="{Binding Path=Choices}"
DisplayMemberPath="Name"
SelectedValue="{Binding Path=Model.CounselExempt, Mode=TwoWay,Converter={StaticResource ConvertBoolToYesNo}}"
SelectedValuePath="Name"
Style="{StaticResource RadioButtonList}" />
【问题讨论】:
-
我想我解决了或至少找到了解决我自己问题的方法。在 SQL 表中,我有一个字段设置为“位”,它可以为空。在我的实体模型中,我将 Nullable 的字段属性设置为“false”。一旦我将它设置为“(无)”,事情就开始起作用了。不知道为什么,我找到了很多关于这个主题的帖子,我很幸运,我的其他位字段之一工作正常,我可以比较设置。
标签: silverlight binding mvvm ivalueconverter