Panos Gr Panos Gr - 1 year ago 183
C# Question

Decimal to Int Converter

I want to make a Conversion, to convert Decimal to Int. Look at my code:

<ProgressBar Margin="0,10,0,10"
Grid.Row="2" Grid.ColumnSpan="3" IsIndeterminate="False" Height="10"
Maximum="{Binding SavingGoal, Converter={StaticResource DecimalToInt}}" Value="{Binding Balance, Converter={StaticResource DecimalToInt}}"/>

And here it's where the bindings pointing at:

newGoal.SavingGoal = Convert.ToDecimal(SavingsAmountsTextBox.Text);
newGoal.Balance = 0;

And here is the class:

public decimal SavingGoal { get; set; }
public decimal Balance { get; set; }

So my problem is that I don't know how to use IValueConverter to fit in my case. I want to convert the decimal that my UserControl is parsing to int and make the progressbar finally to work.

Answer Source

Do you realy need a decimal? The Maximum property and the Value property are doubles, so if this project is about money just use double. If you don't need more than 16 digits after comma, a double makes more sence.

If you still want to convert with a Converter convert it in the Convert()-Method of your Converter Class

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    decimal x = (decimal)value;
    return Decimal.ToInt32(x);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download