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

728x90
반응형
728x170

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

댓글을 달아 주세요