Thanks to levanovd for providing a hint on how to solve this problem. Following is my solution to this problem, thanks again levanovd.
Create a converter
[ValueConversion(typeof(double), typeof(double))]
public class MultiplierConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
if (targetType != typeof(Double))
throw new Exception("Conversion not allowed.");
double f, m = (double)value;
string par = parameter as string;
if (par == null || !Double.TryParse(par, out f)) f = 1;
return m * f;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
if (targetType != typeof(Double))
throw new Exception("Conversion not allowed.");
double f, m = (double)value;
string par = parameter as string;
if (par == null || !Double.TryParse(par, out f)) f = 1;
return f == 0 ? float.NaN : m / f;
}
}
Add converter to your XAML
<Window.Resources>
<n:MultiplierConverter x:Key="MultiplierConverter"/>
</Window.Resources>
Add binding between objects specifying argument for multiplier.
<StackPanel>
<Rectangle x:Name="source" Width="100" Height="100" Stroke="Black"/>
<Rectangle Width="100" Stroke="Black"
Height="{Binding ActualWidth, ElementName=source, Mode=Default,
Converter={StaticResource MultiplierConverter},
ConverterParameter=2}"/>
</StackPanel>
Now the second rectangle will be twice the hight of the first rectangle and can be adjusted with ConverterParameter
. Unfortunately you can't bind ConverterParameter
to another property, not sure why that limitation exists.