■ MultiBinding 엘리먼트 사용하기

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


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

    }

}

 

 

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

    }

}

 

 

MainPage.xaml

 

 

<Page

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:src="clr-namespace:TestProject"

    WindowTitle="MultiBinding 엘리먼트 사용하기">

    <Page.Resources>

        <src:DoubleToByteValueConverter x:Key="DoubleToByteValueConverterKey" />

        <src:RGBToColorValueConverter   x:Key="RGBToColorValueConverterKey"   />

    </Page.Resources>

    <Grid>

        <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="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="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>

</Page>

 

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

Posted by 사용자 icodebroker
TAG