Click here to Skip to main content
12,693,601 members (27,825 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# WPF
Dear All,
I have a field in datacontext as
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.

Posted 23-Mar-13 9:54am
J imran1.3K
ThePhantomUpvoter 23-Mar-13 15:02pm
I think you are going to want to bind to a value converter.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Create custom CheckBox class with overloaded Checked property.

Example for VB.NET:
Custom class definition
Public Class MyCheckBox
    Inherits CheckBox

    Public Overloads Property Checked() As String
            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

        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.
Sandeep Mewara 24-Mar-13 12:26pm
Good one. 5.
Maciej Los 24-Mar-13 12:31pm
Thank you, Sandeep ;)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


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;
                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";
                    return "N";
            return null;

And here's the VM:
public class MainVM: ViewModelBase
     string isChecked;
     public string IsChecked
             return isChecked;
             isChecked = value;

And here's the view:
<window x:class="WpfApplication9.MainWindow" xmlns:x="#unknown">
        Title="MainWindow" Height="350" Width="525">

    public partial class MainWindow : Window
        public MainWindow()
            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:

Hope this helps :)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 26 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100