PrismWPFSample(11)コンバーター追加

Prismを使用したWPFアプリケーション開発で役に立つと思われる項目を一つのアプリケーションにまとめたものを作りました。今回は、コンバーターを追加したので、それについて書いています。

動作環境:Win10, Visual Studio Community 2017, Prism V7.1.0.431, .NET4.5.2, Prism Template Pack, TraceListeners, WPFLocalizeExtension, OxyPlot

アプリの外観はこんな感じです。

f:id:feynman911:20190805193931j:plain


コンバーターに次のものを追加しました。

  • DoubleCheck2BrushConverter
  • StringCheck2BrushConverter
  • BoolVisibilityConverter
  • Bool2CursorWaitConverter
  • BoolInvertConverter

DoubleCheck2BrushConverter

数字の大きさで色を変えたい時に使用します。
パラメータは次のように指定します。
 Blue,3.0,Yellow,6.0,Red
この場合、
3.0よりも小さいとBlue
3.0以上6.0未満でYellow
6.0以上でRedになります。
サンプルでは TextBox の Background にコンバーターを使って数字をバインドしています。

f:id:feynman911:20190806195219j:plain

[ValueConversion(typeof(double), typeof(string))]
public class DoubleCheck2BrushConverter : IValueConverter
{
    // データソース(データ)→バインドターゲット(UI) 変換メソッド
    public object Convert(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culter)
    {
        string br = "Blue";
        double val1 = 3.0;
        double val2 = 6.0;

        double data = (double)value;

        string sp = (string)parameter;
        String[] paras = sp.Split(',');

        double.TryParse(paras[1], out val1);
        if (data < val1)
        {
            if (paras[0] != "") br = paras[0];
            return br;
        }

        double.TryParse(paras[3], out val2);
        if (data < val2)
        {
            if (paras[2] != "") br = paras[2];
            return br;
        }

        if (paras[4] != "") br = paras[4];
        return br;
    }

    // バインドターゲット(UI)→データソース(データ) 変換メソッド
    public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culter)
    {
        return null;
    }
}

StringCheck2BrushConverter

Stringに特定の文字が含まれている時に色を変えるためのコンバータになります。
パラメータは次のようにテキストを指定します。
 ERROR, Red, Blue
この場合、”ERROR"が含まれている時に色を赤にします。含まれていない時にはBlueです。
サンプルでは TextBox の Foreground にコンバーターを使ってViewModelのテキストをバインドしています。

[ValueConversion(typeof(string), typeof(string))]
public class StringCheck2BrushConverter : IValueConverter
{
    /// <summary>
    /// データソース(ViewModel) → バインドターゲット(View)
    /// 例:パラメータ STRING, Red, Black
    /// 文字列に"STRING"が含まれていた時に赤、それ以外は黒
    /// </summary>
    /// <param name="value"></param>
    /// <param name="targetType"></param>
    /// <param name="parameter"></param>
    /// <param name="culture"></param>
    /// <returns></returns>
    public object Convert(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
    {
        string val = (string)value;

        string sp = (string)parameter;
        String[] paras = sp.Split(',');

        if (val.Contains(paras[0]))
        {
            return paras[1];
        }
        return paras[2];
    }

    /// <summary>
    /// バインドターゲット(View)→ データソース(ViewModel)
    /// </summary>
    /// <param name="value"></param>
    /// <param name="targetType"></param>
    /// <param name="parameter"></param>
    /// <param name="culture"></param>
    /// <returns></returns>
    public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

BoolVisibilityConverter

bool値をコンバータを介してVisibilityにバインドすることで、表示したり消したりできます。

[ValueConversion(typeof(bool), typeof(Visibility))]
public class BoolVisiblityConverter : IValueConverter
{
    /// <summary>
    /// データソース(ViewModel) → バインドターゲット(View)
    /// </summary>
    /// <param name="value"></param>
    /// <param name="targetType"></param>
    /// <param name="parameter"></param>
    /// <param name="culture"></param>
    /// <returns></returns>
    public object Convert(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
    {
        bool state = (bool)value;

        if (state)
        {
            return Visibility.Visible;
        }
        else
        {
            return Visibility.Hidden;
        }
    }

    /// <summary>
    /// バインドターゲット(View)→ データソース(ViewModel)
    /// </summary>
    /// <param name="value"></param>
    /// <param name="targetType"></param>
    /// <param name="parameter"></param>
    /// <param name="culture"></param>
    /// <returns></returns>
    public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Bool2CursorWaitConverte

bool値をコンバータを介してCursorにバインドすることで、その部品の上にマウスを持ってきた時にwait状態にすることができます。

f:id:feynman911:20190806195809j:plain

[ValueConversion(typeof(bool), typeof(string))]
public class Bool2CursorWaitConverter : IValueConverter
{
    // データソース(データ)→バインドターゲット(UI) 変換メソッド
    public object Convert(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
    {
        var st = (string)parameter;
        if (true == (bool)value)
        {
            return CursorType.Wait.ToString();
        }
        else
        {
            return CursorType.Arrow.ToString();
        }
    }

    // バインドターゲット(UI)→データソース(データ) 変換メソッド
    public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
    {
        if (CursorType.Wait.ToString() == (string)value)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

BoolInvertConverter

bool値をインバートしてバインドするコンバーターです。

[ValueConversion(typeof(bool), typeof(bool))]
public class BoolInvertConverter : IValueConverter
{
    // データソース(データ)→バインドターゲット(UI) 変換メソッド
    public object Convert(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
    {
        var temp = (bool)value;
        return !temp;
    }

    // バインドターゲット(UI)→データソース(データ) 変換メソッド
    public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
    {
        var temp = (bool)value;
        return !temp;
    }

以上、汎用的に使えそうなコンバーターを追加してみました。

作成したサンプルは次の場所に置いてありますので、詳しくはソースコードを見てもらえればと思います。
github.com