Skip to content

Instantly share code, notes, and snippets.

@kusa-mochi
Created August 7, 2018 02:55
Show Gist options
  • Save kusa-mochi/05f2f12994629414d60efb8926e42ee5 to your computer and use it in GitHub Desktop.
Save kusa-mochi/05f2f12994629414d60efb8926e42ee5 to your computer and use it in GitHub Desktop.
using System;
using System.Globalization;
using System.Windows.Data;
namespace WpfApp1.Converters
{
/// <summary>
/// bool型を"はい"または"いいえ"に変換するコンバータ
/// </summary>
public class BooleanToYesNoConverter : IValueConverter
{
/// <summary>
/// bool型を"はい"または"いいえ"に変換するメソッド
/// </summary>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// 変換元データを変数に受け取る。
var inputBoolean = (bool)value;
// 変換後のデータを返す。
return inputBoolean ? "はい" : "いいえ";
}
/// <summary>
/// "はい"または"いいえ"をbool型に逆変換するメソッド
/// 定義しない場合は return null; だけでOK
/// </summary>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
// 変換元データを変数に受け取る。
var inputString = value as string;
// 変換後のデータを返す。
switch (inputString)
{
case "はい":
return true;
case "いいえ":
return false;
default:
return Binding.DoNothing;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment