Decimal ScrollBar Window with extending IValueConverter

image_pdfimage_print


   
   
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:src="clr-namespace:MyNameSpace.DecimalScrollBar" 
        Title="Decimal ScrollBar">
    <Window.Resources>
        <src:DoubleToDecimalConverter x:Key="conv" />
    </Window.Resources>
    <StackPanel>
        <ScrollBar Name="scroll"
                   Orientation="Horizontal" Margin="24" 
                   Maximum="100" LargeChange="10" SmallChange="1" />
        <Label HorizontalAlignment="Center" 
               Content="{Binding ElementName=scroll, Path=Value, 
                    Converter={StaticResource conv}, ConverterParameter=2}" />
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace MyNameSpace.DecimalScrollBar
{
    [ValueConversion(typeof(double), typeof(decimal))]
    public class DoubleToDecimalConverter : IValueConverter
    {
        public object Convert(object value, Type typeTarget,object param, CultureInfo culture)
        {
            decimal num = new Decimal((double)value);

            if (param != null)
                num = Decimal.Round(num, Int32.Parse(param as string));

            return num;
        }
        public object ConvertBack(object value, Type typeTarget, 
                                  object param, CultureInfo culture)
        {
            return Decimal.ToDouble((decimal)value);
        }
    }
}