Return to Snippet

Revision: 31422
at September 4, 2010 19:26 by jiewmeng


Initial Code
// XAML
<Window.Resources>
    <local:TwoDecimalConverter x:Key="decConverter" />
</Window.Resources>
...
<Slider x:Name="slider1" TickFrequency="1" TickPlacement="BottomRight" />
<TextBox Text="{Binding ElementName=slider1, Path=Value, Converter={StaticResource decConverter}}" /> 

// C#
public class TwoDecimalConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double val;

        if (value == null)
            return "0";

        if (!Double.TryParse(value.ToString(), out val))
            return "0";

        return val.ToString("F2");

    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Initial URL

                                

Initial Description
reference: [blogs.msdn](http://blogs.msdn.com/b/bencon/archive/2006/05/10/594886.aspx)

Initial Title
WPF DataBinding & Converters

Initial Tags

                                

Initial Language
C#