Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi all

I have a piece of code sample which suppose to set the width of the rectangle from a converter. I can see the converter is called twice (the second time with the required value) but the value does not seem to reach the Width property of the rectangle.

note: I use rectangle on purpose instead of a progressbar - requirements

Here is the xaml

XML
<Window.Resources>
    <viewmodel:CalcWidthFromPercentageConverter x:Key="CalcWidthFromPercentageConverter" />
</Window.Resources>
<Window.DataContext>
    <viewmodel:ViewModel/>
</Window.DataContext>
<Grid>
    <Border x:Name="borderParent" Height="20" HorizontalAlignment="Stretch" BorderBrush="Black" BorderThickness="1" VerticalAlignment="Center">
        <Rectangle Fill="Red" Height="20" HorizontalAlignment="Left">
            <Rectangle.Width>
                <MultiBinding Converter="{StaticResource CalcWidthFromPercentageConverter}">
                    <Binding Path="ProgressWidthPercentage"/>
                    <Binding Path="ActualWidth" ElementName="borderParent"></Binding>
                </MultiBinding>
            </Rectangle.Width>
        </Rectangle>
    </Border>
</Grid>



here is the converter

public class CalcWidthFromPercentageConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Count() == 2)
{
decimal inputPercentage = (decimal)values[0];
decimal borderWidth = System.Convert.ToDecimal(values[1]);
decimal result = borderWidth * inputPercentage;
return result;
}
return values;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return new[] { value };
}
}

and last the viewmodel

public class ViewModel
{
private readonly decimal _progressWidthPercentage;

public decimal ProgressWidthPercentage
{
get { return _progressWidthPercentage; }
}

public ViewModel()
{
_progressWidthPercentage = (decimal)0.37;
}
}
Posted
Comments
Shahare 15-Aug-13 4:27am    
It appears I just had to turn the binding property and the converter to double instead of decimal.

WPF does not use decimal as far as I know for any internal values. Many times have to be careful about even using double since there are other properties that are used, like Size or Thickness.
 
Share this answer
 
It appears I just had to turn the binding property and the converter to double instead of decimal.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900