IValueConverter์— MarkupExtension ๋”ํ•˜๊ธฐ

์•ˆ๋…•ํ•˜์„ธ์š”. :smile:

WPF Converter ๊ด€๋ จ ๊ฐ„๋‹จํ•œ ํŒ์ž…๋‹ˆ๋‹ค.

MarkupExtension

WPF Converter๋ฅผ ๋งŒ๋“ค ๋•Œ IValueConverter์™€ ์ด๊ฒƒ์„ ํ•จ๊ป˜ ์ƒ์†๋ฐ›์•„ ๋งŒ๋“ค๊ฒŒ ๋˜๋ฉด
Xaml์—์„œ Converter๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ Resource Key๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค.

BooleanToVisibilityConverter.cs

public class BooleanToVisibilityConverter : MarkupExtension, IValueConverter
{
	public object Convert(object value, Type targetType,
		object parameter, CultureInfo culture)
	{
		return value.Equals(true) ? Visibility.Visible : Visibility.Collapsed;
	}

	public object ConvertBack(object value, Type targetType,
		object parameter, CultureInfo culture)
	{
		throw new NotImplementedException();
	}

	public override object ProvideValue(IServiceProvider serviceProvider)
	{
		return this;
	}
}

Xaml์—์„œ ์‚ฌ์šฉํ•  ๋•Œ

<Border Visibility"="{Binding IsVisible, FallbackValue=Collapsed, 
                              Converter ={cnvts:BooleanToVisibilityConverter}}"/>

์ €๋„ ์ตœ๊ทผ์— ์•Œ๊ณ  ์‚ฌ์šฉํ•ด๋ณด๊ณ  ์žˆ๋Š”๋ฐ์š”.
ํ˜น์‹œ ๋ฌธ์ œ๋˜๋Š” ๋ถ€๋ถ„์ด ์žˆ๊ฑฐ๋‚˜ ์›๋ž˜ ์‚ฌ์šฉํ•˜๊ณ  ๊ณ„์‹  ๋ถ„๋“ค์˜ ์˜๊ฒฌ๋„ ๋“ฃ๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. :smile:

(๊ทธ๋ฆฌ๊ณ  ํ˜น์‹œ ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š” ๋ถ€๋ถ„์ด ์žˆ์„ ๊ฒฝ์šฐ ์งˆ๋ฌธ ๊ฒŒ์‹œํŒ์œผ๋กœ ์˜ฎ๊ธธ ์ˆ˜๋„ ์žˆ์–ด์š”!)

์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

6 Likes

์˜คโ€ฆ!! ์ด๊ฒƒ์€ ์ •๋ง ํŠน๊ธ‰ ๊ฟ€์ด๊ตฐ์š” ใ…Žใ…Žใ…Ž ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค james๋‹˜!

1 Like

๋จธ๋ฆฟ์†์œผ๋ก  "๊ฐ€๋Šฅํ•˜๊ฒ ์ง€?"๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์—ˆ์œผ๋‚˜ ์‹ค์ œ๋กœ ์‹คํ—˜ํ•œ ์ ์ด ์—†์–ด ์ฐธ ๊ถ๊ธˆํ–ˆ๋˜ ๋ถ€๋ถ„์ธ๋ฐ ์‹คํ—˜ํ•ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :slight_smile:

2 Likes

์ €๋Š” Converter ํƒ€์ž…์— MarkupExtension ์˜ ๋ฉค๋ฒ„๊ฐ€ ๋ณด์ด๋Š” ๊ฒŒ ์‹ซ์–ด์„œ ์ด๋Ÿฐ ์‹์œผ๋ฃจ ์“ฐ๊ณ  ์žˆ์–ด์šœ `=ใ…‚=

public abstract class ConverterMarkupExtension<T> : MarkupExtension, IValueConverter
     where T : class, new()
{
    private static Lazy<T> _converter = new Lazy<T>(() => new T());

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return _converter.Value;
    }

    public abstract object Convert(object value, Type targetType, object parameter, CultureInfo culture);

    public abstract object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);
}

public abstract class MultiConverterMarkupExtension<T> : MarkupExtension, IMultiValueConverter
     where T : class, new()
{
    private static Lazy<T> _converter = new Lazy<T>(() => new T());

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return _converter.Value;
    }

    public abstract object Convert(object[] values, Type targetType, object parameter, CultureInfo culture);

    public abstract object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture);
}

public class InversionBoolConverter : ConverterMarkupExtension<InversionBoolConverter>
{
    public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        try
        {
            return (bool)value == false;
        }
        catch
        {
            return Binding.DoNothing;
        }
    }

    public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}


public class CanPanTiltCheckConverter : MultiConverterMarkupExtension<CanPanTiltCheckConverter>
{
    public override object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        try
        {
            var isLive = (bool)values[0];                
            var isDrone = (bool)values[1];
            var isIPconverter = (bool)values[2];

            var canPanTilt = isLive && (isDrone || isIPconverter);

            Debug.WriteLine($"[DebugConverter] IsLive:{isLive}, IsDrone:{isDrone}, IsIPConverter:{isIPconverter}");

            return canPanTilt;
        }
        catch
        {
            return Binding.DoNothing;
        }
    }

    public override object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

5 Likes