0

I saw in this post a solution that fits exactly my needs https://stackoverflow.com/a/8858815/1462911.

But I don't really know how to properly implement it.

I have for now a PositionConverter which converts coordinates in Strings but i'd like to pass through its parameter the ActualWidth of its Parent (a Canvas).

Does my ConverterHelper have to implement IValueConverter and DependencyObject or just DependencyObject?

I'm a bit lost....

Community
  • 1
  • 1
Julien
  • 187
  • 1
  • 2
  • 13
  • Start by telling us why you want to pass the `ActualWidth` as a parameter. Hint: in almost all cases, you can simply use a `MultiBinding` instead of a `Binding` and an `IMultiValueConverter` instead of a simple `IValueConverter`. That will allow you to pass in as many bound values as you like. – Jon Jun 29 '12 at 13:35
  • what i want is to convert a double between 0 and 1 into a string representing a double between 0 and the Parent ActualWidth – Julien Jun 29 '12 at 13:40

1 Answers1

1

What you need is best accomplished through an IMultiValueConverter and a MultiBinding:

public class PositionConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var scale = (double)values[0]; // this is your [0, 1] double
        var max = (double)values[1]; // this is the ActualWidth
        return scale * max;
    }
}

The binding would look like:

<Element.Property>
    <MultiBinding Converter="{StaticResource myConverter}">
        <Binding Path="path_to_the_original_double" />
        <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorLevel=1}"
                 Path="ActualWidth" />
    </MultiBinding>
</Element.Property>
Jon
  • 428,835
  • 81
  • 738
  • 806
  • yeah that's what i was thinking now, using your previous comment. I'll try it right away! thank you for your help!! ^^ – Julien Jun 29 '12 at 13:56
  • A last question, the position converted as string is used to set the Canvas.Left and Top properties of an element i can drag with my mouse. So the binding for the 0 to 1 property is Two Way. But the Binding on ActualWidth is OneWay, How should I implement ConvertBack to manage this? Because ConvertBack should get the Canvas.Left property and Double.Parse it and then divide it by the ActualWidth Property but in the ConvertBack, I can't access it, if i'm correct... – Julien Jun 29 '12 at 14:36
  • @Julien: Two-way conversions won't work because in `ConvertBack` you don't have any way of getting to the parent (and its `ActualWidth`) unless you are willing to do horrible hacks. In this case you 'd be better of wrapping your objects in ViewModels and putting the conversion logic in there (expose values as properties). – Jon Jun 29 '12 at 14:50
  • another problem is added ^^ now i can't pass the ActualValue to the VM since it's a readonly property and a Binding on it is not possible, even if it's a OneWayToSource binding. This is a bug knwon from Microsoft and now i'm searching a workaround for this. – Julien Jun 29 '12 at 16:21
  • i've found a way to handle my problem with ActualWidth [here](http://stackoverflow.com/a/3667609/1462911) ant it works great!! i can go on now. thx for your concern anyway :) – Julien Jul 02 '12 at 08:14