将复选框绑定到具有Y N的字段 [英] bind checkbox to a field having Y N
问题描述
亲爱的所有人,
我在datacontext中有一个字段
nvarchar(1)
这个字段的值是''Y ''或''N''
我想将此字段绑定到wpf中的复选框
我已经使用事件完成了这项任务,但是我需要在Binding上实现这一点以实现标准编码风格
你能帮我绑定一个字符字段到一个复选框。
我知道boolean可以轻松限制但是字符字段呢。
''''用于检查
''N''未经检查。
希望我是有意义。
期待向您提供帮助。
关心
Dear All,
I have a field in datacontext as
nvarchar(1)
the values for this field are ''Y'' or ''N''
I want to bind this field to a checkbox in wpf
I have accomplished this task using events but I need to make this on Binding to acheive standard coding style
Can You please help me binding a character field to a checkbox.
I know boolean can be easily bounded but what about character field.
''Y'' for checked
''N'' For unchecked.
Hope I am making sense.
Looking forward to know from you for the help.
Regards
推荐答案
创建自定义CheckBox
类,重载已检查
属性。
VB.NET示例:
自定义类定义
Create customCheckBox
class with overloadedChecked
property.
Example for VB.NET:
Custom class definition
Public Class MyCheckBox
Inherits CheckBox
Public Overloads Property Checked() As String
Get
Return BooleanToString(MyBase.Checked)
End Get
Set(ByVal value As String)
MyBase.Checked = StringToBoolean(value)
End Set
End Property
Private Function BooleanToString(ByVal b As Boolean) As String
Return IIf(b, "Y", "N")
End Function
Private Function StringToBoolean(ByVal s As String) As Boolean
Return IIf("Y", True, False)
End Function
End Class
用法
Usage
Dim mch As MyCheckBox = New MyCheckBox
With mch
.Location = New Point(8, 8)
.Parent = Me 'this (form)
.Text = "Check it"
'add custom event handler
AddHandler mch.CheckedChanged, AddressOf mcb_CheckedChanged
End With
Private Sub mcb_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles mcb.CheckedChanged
Dim oc As MyCheckBox = sender
Me.Text = oc.Checked.ToString
End Sub
我希望上面的代码也适用于WPF。
I hope that above code can works for WPF too.
您可以使用转换器来完成此操作。就像BoolToVisibilityConverter一样,您可以创建YesNoToBoolConverter。这就是我要怎么做的:
这里是转换器:
Hi,
You can do this by using a converter. Just like a BoolToVisibilityConverter, you can create a YesNoToBoolConverter. Here''s how I would do it:
Here''s the converter:
public class YesNoToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value.ToString() == "Y")
return true;
else
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is bool)
{
if ((bool)value == true)
return "Y";
else
return "N";
}
return null;
}
}
这里是VM:
And here''s the VM:
public class MainVM: ViewModelBase
{
string isChecked;
public string IsChecked
{
get
{
return isChecked;
}
set
{
isChecked = value;
base.RaisePropertyChanged("IsChecked");
}
}
}
这里的观点是:
And here''s the view:
<window x:class="WpfApplication9.MainWindow" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication9"
Title="MainWindow" Height="350" Width="525">
<stackpanel>
<stackpanel.resources>
<local:yesnotobooleanconverter x:key="YesNoConverter" xmlns:local="#unknown" />
</stackpanel.resources>
<checkbox ischecked="{Binding IsChecked, Converter={StaticResource YesNoConverter}}" />
</stackpanel>
</window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainVM() { IsChecked = "N" } ;
}
}
基本上,转换器会截取实际值绑定到您的属性并返回对该属性或视图友好的内容。
这是一个关于价值转换器的好教程:
http://wpftutorial.net/ValueConverters.html
希望这会有所帮助:)
Basically, the converter intercepts the actual value that was bound to your attribute and return something that is friendly to that attribute or to the view.
Here''s a good tutorial about value converter:
http://wpftutorial.net/ValueConverters.html
Hope this helps :)
这篇关于将复选框绑定到具有Y N的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!