■ IMultiValueConverter 인터페이스 사용하기

----------------------------------------------------------------------------------------------------

RGBToColotMultiValueConverter.cs

using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

/// <summary>
///
RGB ↔ 색상 복수 값 변환자
/// </summary>
public class RGBToColorMultiValueConverter : IMultiValueConverter
{
   
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
   ////////////////////////////////////////////////////////////////////////////////////////// Public

   #region 변환하기 - Convert(sourceValueArray, targetType, parameter, cultureInfo)

   
/// <summary>
   /// 변환하기
   /// </summary>
   /// <param name="sourceValueArray">소스 값 배열</param>
   /// <param name="targetType">타겟 타입</param>
   /// <param name="parameter">파라미터</param>
   /// <param name="cultureInfo">CultureInfo</param>
   /// <returns>변환 값</returns>
   public object Convert(object[] sourceValueArray, Type targetType, object parameter, CultureInfo cultureInfo)
   {
       
Color color = Color.FromRgb
       (
           (
byte)(double)sourceValueArray[0],
           (
byte)(double)sourceValueArray[1],
           (
byte)(double)sourceValueArray[2]
       );

       
if(targetType == typeof(Color))
       {
           
return color;
       }

       
if(targetType == typeof(Brush))
       {
           
return new SolidColorBrush(color);
       }

       
return null;
   }

   #endregion

   #region
역변환하기 - ConvertBack(sourceValue, targetTypeArray, parameter, cultureInfo)

   
/// <summary>
   /// 역변환하기
   /// </summary>
   /// <param name="sourceValue">소스 값</param>
   /// <param name="targetTypeArray">타겟 타입 배열</param>
   /// <param name="parameter">파라미터</param>
   /// <param name="cultureInfo">CultureInfo</param>
   /// <returns>역변환 값</returns>
   public object[] ConvertBack(object sourceValue, Type[] targetTypeArray, object parameter, CultureInfo cultureInfo)
   {
       
Color color;

       
object[] targetValueArray = new object[3];

       
if(sourceValue is Color)
       {
           color = (
Color)sourceValue;
       }
       
else if(sourceValue is SolidColorBrush)
       {
           color = (sourceValue
as SolidColorBrush).Color;
       }
       
else
       {
           
return null;
       }

       targetValueArray[0] = color.R;
       targetValueArray[1] = color.G;
       targetValueArray[2] = color.B;

       
return targetValueArray;
   }

   #endregion
}

TestPage.xaml

<Page
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:src="clr-namespace:WpfApplication1"
   WindowTitle="색상 스크롤">
   <Page.Resources>
       <src:DoubleToByteValueConverter x:Key="DoubleToByteValueConverterKey" />
       <src:RGBToColorMultiValueConverter x:Key="RGBToColorMultiValueConverterKey" />
   </Page.Resources>
   <Grid>
       <Grid.ColumnDefinitions>
           <ColumnDefinition Width="*"    />
           <ColumnDefinition Width="Auto" />
           <ColumnDefinition Width="*"    />
       </Grid.ColumnDefinitions>
       <Grid Grid.Column="0">
           <Grid.RowDefinitions>
               <RowDefinition Height="Auto" />
               <RowDefinition Height="100*" />
               <RowDefinition Height="Auto" />
           </Grid.RowDefinitions>
           <Grid.ColumnDefinitions>
               <ColumnDefinition Width="33*" />
               <ColumnDefinition Width="33*" />
               <ColumnDefinition Width="33*" />
           </Grid.ColumnDefinitions>
           <Label
               Grid.Row="0"
               Grid.Column="0"
               HorizontalAlignment="Center"
               Foreground="Red"
               Content="빨강" />
           <ScrollBar
               Name="redScrollBar"
               Grid.Row="1"
               Grid.Column="0"
               Background="Red"
               Minimum="0"
               Maximum="255"
               SmallChange="1"
               LargeChange="16"
               Focusable="True"
               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="녹색" />
           <ScrollBar
               Name="greenScrollBar"
               Grid.Row="1"
               Grid.Column="1"
               Background="Green"
               Minimum="0"
               Maximum="255"
               SmallChange="1"
               LargeChange="16"
               Focusable="True"
               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="파랑" />
           <ScrollBar
               Name="blueScrollBar"
               Grid.Row="1"
               Grid.Column="2"
               Background="Blue"
               Minimum="0"
               Maximum="255"
               SmallChange="1"
               LargeChange="16"
               Focusable="True"
               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.Column="1"
           HorizontalAlignment="Center"
           VerticalAlignment="Stretch"
           Width="6" />
       <Border Grid.Column="2">
           <Border.Background>
               <MultiBinding Converter="{StaticResource RGBToColorMultiValueConverterKey}">
                   <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>
</
Page>
----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요