์๋
ํ์ธ์.
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}}"/>
์ ๋ ์ต๊ทผ์ ์๊ณ ์ฌ์ฉํด๋ณด๊ณ ์๋๋ฐ์.
ํน์ ๋ฌธ์ ๋๋ ๋ถ๋ถ์ด ์๊ฑฐ๋ ์๋ ์ฌ์ฉํ๊ณ ๊ณ์ ๋ถ๋ค์ ์๊ฒฌ๋ ๋ฃ๊ณ ์ถ์ต๋๋ค.
(๊ทธ๋ฆฌ๊ณ ํน์ ๋ฌธ์ ๊ฐ ์๋ ๋ถ๋ถ์ด ์์ ๊ฒฝ์ฐ ์ง๋ฌธ ๊ฒ์ํ์ผ๋ก ์ฎ๊ธธ ์๋ ์์ด์!)
์ฝ์ด์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค.
6 Likes
์คโฆ!! ์ด๊ฒ์ ์ ๋ง ํน๊ธ ๊ฟ์ด๊ตฐ์ ใ
ใ
ใ
๊ฐ์ฌํฉ๋๋ค james๋!
1 Like
๋จธ๋ฆฟ์์ผ๋ก "๊ฐ๋ฅํ๊ฒ ์ง?"๋ผ๊ณ ์๊ฐํ์์ผ๋ ์ค์ ๋ก ์คํํ ์ ์ด ์์ด ์ฐธ ๊ถ๊ธํ๋ ๋ถ๋ถ์ธ๋ฐ ์คํํด์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค
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