DecimalPercentageConverter

        <StackPanel x:Name="StackPanelPerson" Grid.Row="1" Width="200">
            <TextBox Text="{Binding Mode=TwoWay, Path=UnitPrice, ValidatesOnExceptions=True, NotifyOnValidationError=True, StringFormat=\{0:F\}}"/>
            <TextBox Height="23" Text="{Binding Mode=TwoWay, Path=UnitPriceRate, ValidatesOnExceptions=True, NotifyOnValidationError=True, Converter={StaticResource DecimalPercentageConverter}, StringFormat=\{0:P\}}" />
            <TextBox Text="{Binding Mode=TwoWay, Path=SuggestPrice, ValidatesOnExceptions=True, NotifyOnValidationError=True, StringFormat=\{0:F\}}"/>
            <TextBox Height="23" Text="{Binding Mode=TwoWay, Path=SuggestPriceRate, ValidatesOnExceptions=True, NotifyOnValidationError=True, Converter={StaticResource DecimalPercentageConverter}, StringFormat=\{0:P\}}" />
            <sdk:Label x:Name="LabelMessage" />
            <Button Content="test" Click="Button_Click_1" />
        </StackPanel>

     public class DecimalPercentageConverter : IValueConverter

    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(decimal) || value == null)
                return value;

            string str = value.ToString();

            if (String.IsNullOrWhiteSpace(str))
                return 0M;

            str = str.TrimEnd(culture.NumberFormat.PercentSymbol.ToCharArray());

            decimal result = 0M;
            if (decimal.TryParse(str, out result))
            {
                result /= 100;
            }
            return result;
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章