■ MultiBinding 엘리먼트의 Converter 속성에서 IMultiValueConverter 인터페이스 구현 객체를 사용해 바인딩하는 방법을 보여준다.
▶ DoubleToByteValueConverter.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
using System; using System.Globalization; using System.Windows.Data; namespace TestProject { /// <summary> /// Double↔Byte 값 변환자 /// </summary> [ValueConversion(typeof(double), typeof(byte))] public class DoubleToByteValueConverter : IValueConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 변환하기 - Convert(value, targetType, parameter, cultureInfo) /// <summary> /// 변환하기 /// </summary> /// <param name="value">값</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">문화 정보</param> /// <returns>변환 값</returns> public object Convert(object value, Type targetType, object parameter, CultureInfo cultureInfo) { return (byte)(double)value; } #endregion #region 역변환하기 - ConvertBack(value, targetType, parameter, cultureInfo) /// <summary> /// 역변환하기 /// </summary> /// <param name="value">값</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">문화 정보</param> /// <returns>역변환 값</returns> public object ConvertBack(object value, Type targetType, object parameter, CultureInfo cultureInfo) { return (double)value; } #endregion } } |
▶ RGBToColorValueConverter.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
using System; using System.Globalization; using System.Windows.Data; using System.Windows.Media; namespace TestProject { /// <summary> /// RGB↔Color 값 변환자 /// </summary> public class RGBToColorValueConverter : IMultiValueConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 변환하기 - Convert(valueArray, targetType, parameter, cultureInfo) /// <summary> /// 변환하기 /// </summary> /// <param name="valueArray">값 배열</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">문화 정보</param> /// <returns>변환 값</returns> public object Convert(object[] valueArray, Type targetType, object parameter, CultureInfo cultureInfo) { Color color = Color.FromRgb ( (byte)(double)valueArray[0], (byte)(double)valueArray[1], (byte)(double)valueArray[2] ); if(targetType == typeof(Color)) { return color; } if(targetType == typeof(Brush)) { return new SolidColorBrush(color); } return null; } #endregion #region 역변환하기 - ConvertBack(value, targetTypeArray, parameter, cultureInfo) /// <summary> /// 역변환하기 /// </summary> /// <param name="value">값</param> /// <param name="targetTypeArray">타겟 타입 배열</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">문화 정보</param> /// <returns>역변환 값</returns> public object[] ConvertBack(object value, Type[] targetTypeArray, object parameter, CultureInfo cultureInfo) { Color color; if(value is Color) { color = (Color)value; } else if(value is SolidColorBrush) { color = (value as SolidColorBrush).Color; } else { return null; } object[] primaryValueArray = new object[3]; primaryValueArray[0] = color.R; primaryValueArray[1] = color.G; primaryValueArray[2] = color.B; return primaryValueArray; } #endregion } } |
▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="MultiBinding 엘리먼트 : Converter 속성에서 IMultiValueConverter 인터페이스 구현 객체를 사용해 바인딩하기" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <local:DoubleToByteValueConverter x:Key="DoubleToByteValueConverterKey" /> <local:RGBToColorValueConverter x:Key="RGBToColorValueConverterKey" /> </Window.Resources> <Grid Margin="10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid Grid.Row="0" Grid.Column="0"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center" Foreground="Red" Content="Red" /> <ScrollBar Name="redScrollBar" Grid.Row="1" Grid.Column="0" Background="Red" Focusable="True" Minimum="0" Maximum="255" SmallChange="1" LargeChange="16" Value="128" /> <Label Grid.Row="2" Grid.Column="0" HorizontalAlignment="Center" Content="{Binding ElementName=redScrollBar, Path=Value, Mode=OneWay, Converter={StaticResource DoubleToByteValueConverterKey}}" /> <Label Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center" Foreground="Green" Content="Green" /> <ScrollBar Name="greenScrollBar" Grid.Row="1" Grid.Column="1" Background="Green" Focusable="True" Minimum="0" Maximum="255" SmallChange="1" LargeChange="16" Value="128" /> <Label Grid.Row="2" Grid.Column="1" HorizontalAlignment="Center" Content="{Binding ElementName=greenScrollBar, Path=Value, Mode=OneWay, Converter={StaticResource DoubleToByteValueConverterKey}}" /> <Label Grid.Row="0" Grid.Column="2" HorizontalAlignment="Center" Foreground="Blue" Content="Blue" /> <ScrollBar Name="blueScrollBar" Grid.Row="1" Grid.Column="2" Background="Blue" Focusable="True" Minimum="0" Maximum="255" SmallChange="1" LargeChange="16" Value="128" /> <Label Grid.Row="2" Grid.Column="2" HorizontalAlignment="Center" Content="{Binding ElementName=blueScrollBar, Path=Value, Mode=OneWay, Converter={StaticResource DoubleToByteValueConverterKey}}" /> </Grid> <GridSplitter Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Stretch" Width="6" /> <Border Grid.Row="0" Grid.Column="2"> <Border.Background> <MultiBinding Converter="{StaticResource RGBToColorValueConverterKey}"> <Binding ElementName="redScrollBar" Path="Value" Mode="OneWay "/> <Binding ElementName="greenScrollBar" Path="Value" Mode="OneWay "/> <Binding ElementName="blueScrollBar" Path="Value" Mode="OneWay "/> </MultiBinding> </Border.Background> </Border> </Grid> </Window> |