带有 Bool To YesNo IValueConverter 的 ListBox...几乎可以工作,但不完全有效
我正在尝试使用列表框来显示是/否并绑定到我的 Sql Server 数据库中的位字段。
我的代码(尤其是我的绑定)看起来正确吗?
使用此功能时,它可以使用“是”(真)语句或“否”(假)语句创建记录,我什至可以在数据库中将“否”(假)更新为“是”(真)......但我可以不要将“是”(真)编辑为“否”(假)...如果我调试一切看起来都不错,直到我点击 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}" />
I'm attempting to use a Listbox to display Yes/No and bind to a bit field in my Sql Server Database.
Does my code (especially my binding) look correct?
When using this, it work for creating a record with a Yes (true) statement, or No (false) statement, I can even update a No (false) to a Yes (true) in the database....but I can't edit a Yes (true) to a No (False)...if I debug everything looks good, right up until I hit my RiaService, then using Sql Profiler, the field isn't updated.
Oh yeah, If I bind this same code to a varchar field and exclude the converter, everything works fine.
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}" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您可以对其进行调试并在调用 RIA 服务之前看到该值已更改,那么这不是值转换器问题。更有可能的是,对象到服务器端的传输/序列化有些模糊。
If you can debug it and see that the value is changed right before you make the call to your RIA Service, then it isn't a valueconverter issue. More likely is it something fuzzy with the transport/serialization of the object to the server side.