첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ Binding 클래스 : 특정 엘리먼트 속성 바인딩하기

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


TestProject.zip


DoubleToDecimalValueConveter.cs

 

 

using System;

using System.Globalization;

using System.Windows.Data;

 

namespace TestProject

{

    /// <summary>

    /// 실수↔10진수 값 변환자

    /// </summary>

    [ValueConversion(typeof(double), typeof(decimal))]

    public class DoubleToDecimalValueConverter : IValueConverter

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

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

 

        /// <summary>

        /// 변환하기

        /// </summary>

        /// <param name="sourceValue"></param>

        /// <param name="targetType">타겟 타입</param>

        /// <param name="parameter">파라미터</param>

        /// <param name="cultureInfo">CultureInfo</param>

        /// <returns>변환 값</returns>

        public object Convert(object sourceValue, Type targetType, object parameter, CultureInfo cultureInfo)

        {

            decimal targetValue = new decimal((double)sourceValue);

 

            if(parameter != null)

            {

                targetValue = decimal.Round(targetValue, int.Parse(parameter as string));

            }

 

            return targetValue;

        }

 

        #endregion

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

 

        /// <summary>

        /// 역변환하기

        /// </summary>

        /// <param name="sourceValue">소스 값</param>

        /// <param name="targetType">타겟 타입</param>

        /// <param name="parameter">파라미터</param>

        /// <param name="cultureInfo">CultureInfo</param>

        /// <returns>역변환 값</returns>

        public object ConvertBack(object sourceValue, Type targetType, object parameter, CultureInfo cultureInfo)

        {

            return decimal.ToDouble((decimal)sourceValue);

        }

 

        #endregion

    }

}

 

 

MainWindow.xaml

 

 

<Window x:Class="TestProject.MainWindow"

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

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

    Width="800"

    Height="600"

    Title="Binding 엘리먼트 : ElementName 속성을 사용해 특정 엘리먼트 속성 바인딩하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <StackPanel

        HorizontalAlignment="Center"

        VerticalAlignment="Center">

        <ScrollBar Name="scrollBar"

            HorizontalAlignment="Center"

            Margin="10"

            Width="500"

            Height="20"

            Orientation="Horizontal"

            Maximum="100"

            SmallChange="1"

            LargeChange="10" />

        <Label Name="label"

            HorizontalAlignment="Center"

            Margin="10">

        </Label>

    </StackPanel>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System.Windows;

using System.Windows.Controls;

using System.Windows.Controls.Primitives;

using System.Windows.Data;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            Binding binding = new Binding();

 

            binding.Source              = this.scrollBar;

            binding.Path                = new PropertyPath(ScrollBar.ValueProperty);

            binding.Mode                = BindingMode.OneWay;

            binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

            binding.Converter           = new DoubleToDecimalValueConverter();

            binding.ConverterParameter  = "2";

 

            this.label.SetBinding(Label.ContentProperty, binding);

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG ,

댓글을 달아 주세요