Click here to Skip to main content
16,012,223 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
[no name] 23-Mar-13 15:02pm    
I think you are going to want to bind to a value converter.

Create custom CheckBox class with overloaded Checked property.

Example for VB.NET:
Custom class definition
VB
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
VB
    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


I hope that above code can works for WPF too.
 
Share this answer
 
Comments
Sandeep Mewara 24-Mar-13 12:26pm    
Good one. 5.
Maciej Los 24-Mar-13 12:31pm    
Thank you, Sandeep ;)
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:
C#
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;
        }
    }



And here's the VM:
C#
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>

C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new MainVM() { IsChecked = "N" } ;

    }
}


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 :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900