첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형

TestProject.zip
다운로드

▶ DoubleToByteValueConverter.cs

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
    }
}

 

728x90

 

▶ RGBToColorValueConverter.cs

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
    }
}

 

300x250

 

▶ MainPage.xaml

<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>
728x90
반응형
그리드형(광고전용)
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요