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;
}
}
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" } ;
}
}
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 :)